简体   繁体   English

如何忽略asp.net中的路由表单url路由

[英]How to ignore route in asp.net forms url routing

I am using the .NET 3.5 SP1 framework and I've implemented URL routing in my application. 我正在使用.NET 3.5 SP1框架,并在我的应用程序中实现了URL路由。 I was getting javascript errors: 我收到了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

Which I believe is because my routing is picking up the microsoft axd files and not properly sending down the javascript. 我相信这是因为我的路由选择了微软的axd文件而没有正确发送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 ,这应该允许我忽略下面的Routes.IgnoreRoute

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?)

I've got the System.Web.Routing namespace imported, any ideas? 我已经导入了System.Web.Routing命名空间,有什么想法吗?

You don't need to reference ASP.NET MVC. 您不需要引用ASP.NET MVC。 You can use the StopRoutingHandler which implements IRouteHandler like so: 您可以使用实现IRouteHandler的StopRoutingHandler ,如下所示:

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

This is part of .NET 3.5 SP1 and doesn't require MVC. 这是.NET 3.5 SP1的一部分,不需要MVC。 The IgnoreRoutes method is a convenience extension method which is part of ASP.NET MVC. IgnoreRoutes方法是一种便捷扩展方法,它是ASP.NET MVC的一部分。

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 (ie, not using MVC). 存在“忽略”方法,而在标准ASP.NET中,“IgnoreRoute”方法似乎不显示(即,不使用MVC)。 This will achieve the same result as Haacked's code, but is slightly cleaner ... 这将获得与Haacked代码相同的结果,但稍微清晰一点......

我只想补充一点,你还需要确保IgnoreRoutes规则的顺序是正确的顺序,否则你的第一条路线将首先应用,你的IgnoreRoute将被忽略。

MapRoute和IgnoreRoute是System.Web.Mvc中的扩展方法---您是否正确引用了该程序集?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM