最近做ASP.NET MVC项目时,发现有些页面的图片链接被MVC Route解析成Cotnroller进行处理,提示错误:
The controller for path ‘/Content/images/xxx.gif’ could not be found or it does not implement the IController interface.
参数名: controllerType
于是修改了Global.asax.cs中的代码:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute(“{resource}.axd/{*pathInfo}”);
routes.IgnoreRoute(“{*allgif}”, new { alljs = @”.*\.gif(/.*)?” });//加入了路由解析规则,让ASP.NET默认的解析机制进行解析,跳过ASP.NET MVC的Cotroller解析器避免出现上述错误
routes.MapRoute(
“Default”, // Route name
“{controller}/{action}/{id}”, // URL with parameters
new { controller = “Home”, action = “Index”, id = “” } // Parameter defaults
);
}
备注:IgnoreRoute 写法:
routes.IgnoreRoute(“{*过滤规则名称}”, new { 过滤规则名称 = @”正则表达式” });
实例:routes.IgnoreRoute(“{*allgif}”, new { alljs = @”.*\.gif(/.*)?” });
具体IgnoreRoute的使用规则可以阅读下面两篇文章:
ASP.NET MVC Best Practices (Part 2) /
Routing MSDN文档说明:
http://msdn.microsoft.com/en-us/library/system.web.routing.aspx
http://msdn.microsoft.com/en-us/library/system.web.routing.routecollection.aspx