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

时间:2008-11-07 20:01:33

标签: c# asp.net url-routing axd ignoreroute

我正在使用.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}");

但是,当我将该行添加到我的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命名空间,有什么想法吗?

4 个答案:

答案 0 :(得分:39)

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

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

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

答案 1 :(得分:8)

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

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

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

答案 2 :(得分:3)

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

答案 3 :(得分:1)

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