在asp.net mvc2之后的版本里面,有了area(区域的概念),这为咱们开发中提供了很多方便的地方,可是很不凑巧,如果存在多个重名的controller就会发生错误,所以咱们能够根据router的规则来解决这个问题。mvc
错误提示:asp.net
咱们能够根据错误提示来解决这个问题,在MapRoute方法添加namespaces参数,代码以下:url
1 public static void RegisterRoutes(RouteCollection routes) 2 { 3 routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 4 5 routes.MapRoute( 6 name: "Default", 7 url: "{controller}/{action}/{id}", 8 defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }, 9 namespaces:new []{"MvcApplication8.Controllers"} 10 ); 11 }
加上命名空间后,就会按照命名空间去找相应的控制器,这个问题就解决了。spa
解决这个问题,咱们能够使用Route对象的DataTokens属性来指定命名空间列表。DataTokens.Add()方法,咱们能够理解为存储于Route对象的DataTokens属性中,对应的Key为“Namespaces”。这两个问题的解决方法有类似之处。.net
代码:3d
1 public static void RegisterRoutes(RouteCollection routes) 2 { 3 routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 4 5 routes.MapRoute( 6 name: "Default", 7 url: "{controller}/{action}/{id}", 8 defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }, 9 namespaces: new[] { "MvcApplication8.Controllers" } 10 ).DataTokens.Add("area","admin"); 11 }
其中defaults里面的controller和action为area里面设置起始页面的controller和action方法。code
不少人为这个默认起始页发愁,都找不到!router