C# 如何忽略asp.net表单url路由中的路由
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/273447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to ignore route in asp.net forms url routing
提问by Austin
I am using the .NET 3.5 SP1 framework and I've implemented URL routing in my application. I was getting javascript errors:
我正在使用 .NET 3.5 SP1 框架,并且在我的应用程序中实现了 URL 路由。我收到了 javascript 错误:
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
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:
我相信这是因为我的路由选择了 microsoft axd 文件并且没有正确发送 javascript。我做了一些研究,发现我可以使用Routes.IgnoreRoute
,这应该允许我忽略像下面这样的 axd:
Routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
But, when I add that line to my Global.asax I get this error:
但是,当我将该行添加到 Global.asax 时,出现此错误:
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?)
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?
我已经System.Web.Routing
导入了命名空间,有什么想法吗?
采纳答案by Haacked
You don't need to reference ASP.NET MVC. You can use the StopRoutingHandlerwhich implements IRouteHandler like so:
您不需要引用 ASP.NET MVC。您可以使用实现 IRouteHandler的StopRoutingHandler,如下所示:
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.
这是 .NET 3.5 SP1 的一部分,不需要 MVC。IgnoreRoutes 方法是一种方便的扩展方法,它是 ASP.NET MVC 的一部分。
回答by Dan Esparza
MapRoute and IgnoreRoute are extension methods in System.Web.Mvc --- do you have that assembly referenced properly?
MapRoute 和 IgnoreRoute 是 System.Web.Mvc 中的扩展方法——您是否正确引用了该程序集?
回答by Omenof
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.
我只想补充一点,您还需要确保 IgnoreRoutes 规则的顺序正确,否则将首先应用您的第一条路线,而您的 IgnoreRoute 将……被忽略。
回答by Ed Graham
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 ...
“Ignore”方法存在,而在标准 ASP.NET 中,“IgnoreRoute”方法似乎不存在(即,不使用 MVC)。这将实现与 Haacked 的代码相同的结果,但更简洁......