我正在使用.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导入的命名空间,任何想法?
RouteCollection.Ignore(url, constraints)和之间有什么区别RouteCollection.IgnoreRoute(url, constraints)?
新的MVC项目IgnoreRoute在Global.asax RegisterRoutes方法中包含此调用,以跳过对ASP.NET系统中其他位置处理的.axd位置的请求的路由.
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
Run Code Online (Sandbox Code Playgroud)
我想为项目添加一个额外忽略的路由,然后我开始输入新行.之后routes.I,Intellisense弹出,.Ignore并且.IgnoreRoute两者听起来都差不多.
根据MSDN文档,您可以看到一个是类的实例方法,System.Web.Routing.RouteCollection另一个是该类的扩展方法System.Web.Mvc.RouteCollectionExtensions.
RouteCollection.Ignore:"如果请求URL满足指定的约束,则定义不应检查路由匹配的URL模式"(MSDN文档).RouteCollection.IgnoreRoute:"忽略给定的可用路由列表和约束列表的指定URL路由"(MSDN文档).两者都采用路由URL模式和一组约束来限制路由在该URL模式上的应用.
无论路径信息如何,我都想阻止对任何.php或.cgi的请求.
例如,使用以下URL时:
http:// mysite /Admin/Scripts/Setup.php
它匹配现有路线:
routeCollection.MapRoute("Admin", "admin/{controller}/{action}/{uid}/{*pathInfo}", new { controller = "Admin", action = "Index", uid = "" });
Run Code Online (Sandbox Code Playgroud)
但是没有脚本控制器,所以MVC抛出以下内容:
IControllerFactory''没有返回名为'scripts'的控制器的控制器.
我真正喜欢的是,在MVC进入控制器之前,请求只是遇到了一次硬故障.
我知道我可以通过在Global.asax中挂钩Application_BeginRequest并抛出一个新的HttpException(404,"Not Found")来做到这一点,但这不是我想要的优雅解决方案.
我真的希望这会奏效:
routeCollection.IgnoreRoute("{resource}.php/{*pathInfo}");
Run Code Online (Sandbox Code Playgroud)
但事实并非如此.
注意:Sean Lynch的答案很有效,但我仍然非常喜欢基于System.Web.Routing或System.Web.Mvc的解决方案.这样我就可以允许我的用户在运行时添加自己的排除项.
默认的ASP.NET MVC 3项目模板包含以下IgnoreRoute指令:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
Run Code Online (Sandbox Code Playgroud)
我现在看到多个项目改变了这一行(包括StackExchange的DataExplorer),而不是看起来像:
routes.IgnoreRoute("{*allaxd}", new {allaxd = @".*\.axd(/.*)?"});
Run Code Online (Sandbox Code Playgroud)
任何人都可以解释在什么情况下或一般情况下为什么默认.axd路由忽略不适合后一个版本? 或者反过来说,为什么人们会选择不使用后一个版本而只是坚持默认?
我试图访问views目录中的.js文件.我有一个带有/Views/Home/MyControl.ascx的MVC应用程序我有一个js文件/Views/Home/MyControl.js
我希望引用.js文件并将其与控件保持一致.我在路由中尝试了以下条目,似乎都没有工作.
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{file}.js");
routes.IgnoreRoute("{resource}.js/{*pathInfo}");
routes.IgnoreRoute("{controller}/{resource}.js/{*pathInfo}");
routes.IgnoreRoute("{*alljs}", new { alljs = @".*\.js(/.*)?" });
Run Code Online (Sandbox Code Playgroud)
请帮助,请不要建议将.js文件添加到脚本目录中.我想以这种方式工作,或者知道为什么不能这样做.
我会把脚本放到页面中,只在VS2010 B2中打破脚本调试.
谢谢关心克雷格.