How to ignore route in asp.net forms url routing

回眸只為那壹抹淺笑 提交于 2019-11-26 13:17:47

问题


I am using the .NET 3.5 SP1 framework and I've implemented URL routing in my application. I was getting javascript errors:

Error: ASP.NET Ajax client-side framework failed to load.
Resource interpreted as script but transferred with MIME type text/html.
ReferenceError: Can't find variable: Sys

Which I believe is because my routing is picking up the microsoft axd files and not properly sending down the javascript. I did some research and found that I could use Routes.IgnoreRoute, which should allow me to ignore the axd like below:

Routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

But, when I add that line to my Global.asax I get this error:

CS1061: 'System.Web.Routing.RouteCollection' does not contain a definition for 'IgnoreRoute' and no extension method 'IgnoreRoute' accepting a first argument of type 'System.Web.Routing.RouteCollection' could be found (are you missing a using directive or an assembly reference?)

I've got the System.Web.Routing namespace imported, any ideas?


回答1:


You don't need to reference ASP.NET MVC. You can use the StopRoutingHandler which implements IRouteHandler like so:

routes.Add(new Route("{resource}.axd/{*pathInfo}", new StopRoutingHandler()));

This is part of .NET 3.5 SP1 and doesn't require MVC. The IgnoreRoutes method is a convenience extension method which is part of ASP.NET MVC.




回答2:


An old question but in case it still helps anyone, this worked for me:

routes.Ignore("{resource}.axd/{*pathInfo}");

The "Ignore" method exists, whereas in standard ASP.NET the "IgnoreRoute" method appears not to (i.e., not using MVC). This will achieve the same result as Haacked's code, but is slightly cleaner ...




回答3:


I would just like to add that you also need to make sure the order of your IgnoreRoutes rule is in the the correct order otherwise your first route will be applied first and your IgnoreRoute will... well be ignored.




回答4:


MapRoute and IgnoreRoute are extension methods in System.Web.Mvc --- do you have that assembly referenced properly?



来源:https://stackoverflow.com/questions/273447/how-to-ignore-route-in-asp-net-forms-url-routing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!