Asp.net MVC 4新項目中創建area后,往往HomeController與area的HomeController路由發生混淆,需要手工設置一些地方避免mvc無法識別默認路由的狀況。
無廢話具體步驟:
1. 檢查早Global.asax和\App_Start\RouteConfig.cs中是否已經自動添加了AreaRegistration.RegisterAllAreas();如不存在,進入第2步,否則第3步
2. 在\App_Start\RouteConfig.cs中,添加AreaRegistration.RegisterAllAreas();
public static void RegisterRoutes(RouteCollection routes) {routes.IgnoreRoute("{resource}.axd/{*pathInfo}");AreaRegistration.RegisterAllAreas();routes.MapRoute(name: "Default",url: "{controller}/{action}/{id}",defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },namespaces: new[] { "TestMvcApplication.Controllers" }); }
3.?在\App_Start\RouteConfig.cs中,檢查routes.MapRoute()中是否顯性指定了默認Controller的namespace。
namespaces: new[] { "TestMvcApplication.Controllers" }
?