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

Aus*_*tin 22 c# asp.net url-routing ignoreroute axd

我正在使用.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

我相信这是因为我的路由选择了微软的axd文件而没有正确发送javascript.我做了一些研究,发现我可以使用Routes.IgnoreRoute,这应该允许我忽略下面的axd:

Routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
Run Code Online (Sandbox Code Playgroud)

但是,当我将该行添加到我的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?)

我有System.Web.Routing导入的命名空间,任何想法?

Haa*_*ked 39

您不需要引用ASP.NET MVC.您可以使用实现IRouteHandler 的StopRoutingHandler,如下所示:

routes.Add(new Route("{resource}.axd/{*pathInfo}", new StopRoutingHandler()));
Run Code Online (Sandbox Code Playgroud)

这是.NET 3.5 SP1的一部分,不需要MVC.IgnoreRoutes方法是一种便捷扩展方法,它是ASP.NET MVC的一部分.


Ed *_*ham 8

一个老问题,但万一它仍然可以帮助任何人,这对我有用:

routes.Ignore("{resource}.axd/{*pathInfo}");
Run Code Online (Sandbox Code Playgroud)

存在"忽略"方法,而在标准ASP.NET中,"IgnoreRoute"方法似乎不显示(即,不使用MVC).这将获得与Haacked代码相同的结果,但稍微清晰一点......