·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> Asp.netMVC4新项目中创建area的后续操作

Asp.netMVC4新项目中创建area的后续操作

作者:佚名      ASP.NET网站开发编辑:admin      更新时间:2022-07-23

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" }