2008-11-07 65 views
22

我使用.NET 3.5 SP1框架,并且在我的应用程序中实现了URL路由。我得到的JavaScript错误:如何忽略asp.net表单中的路由url routing

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命名空间导入,任何想法?

回答

39

您不需要引用ASP.NET MVC。你可以用它实现IRouteHandler像这样的StopRoutingHandler

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

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

+0

谢谢,这是一个巨大的帮助。 – Austin 2008-11-10 14:20:09

+0

如何忽略ASP.NET Core(MVC 6)中的* .php文件?它有很大的不同! – jp2code 2016-06-21 13:27:59

1

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

+0

我没有使用MVC,所以我没有引用该程序集。我需要单独下载MVC程序集才能使用IgnoreRoute,或者它应该是.NET 3.5 SP1的一部分吗? – Austin 2008-11-07 20:56:36

3

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

8

一个老问题,但在情况下,它仍然可以帮助任何人,这个工作对我来说:

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

“忽略”方式存在,而在标准的ASP.NET出现“IgnoreRoute”法不(即,不使用MVC)。这将达到与Haacked的代码相同的结果,但稍微更清洁...