不同区域中的控制器名称相同

Kri*_*s-I 8 asp.net-mvc

在具有区域的ASP.NET MVC 3应用程序中(请参阅下面的架构).根目录中的"Controllers"目录已被删除.

当我这样做:

    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
    );
Run Code Online (Sandbox Code Playgroud)

我收到此错误: 找到了与名为"Home"的控制器匹配的多个类型.如果为此请求提供服务的路由('{controller}/{action}/{id}')未指定名称空间来搜索与请求匹配的控制器,则会发生这种情况.如果是这种情况,请通过调用带有'namespaces'参数的'MapRoute'方法的重载来注册此路由.对'Home'的请求找到了以下匹配的控制器:MyProject.Areas.Administration.Controllers.HomeController MyProject.Areas.BackEnd.Controllers.HomeController MyProject.Areas.FrontEnd.Controllers.HomeController

当我这样做:

    routes.MapRoute(
         "Default", // Route name
         "{controller}/{action}/{id}", // URL with parameters
         new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
         new string[] { "MyProject.Areas.Administration.Controllers" }
    );
Run Code Online (Sandbox Code Playgroud)

我得到了错误:

The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Home/Index.aspx
~/Views/Home/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Views/Home/Index.cshtml
~/Views/Home/Index.vbhtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Index.vbhtml


---- Areas
       |
       |---- Administration
       |       |--- Controllers
       |       |      |---- HomeController
       |       |--- Views
       |              |--- Index
       |---- FrontEnd
       |       |--- Controllers
       |       |      |---- HomeController
       |       |--- Views
       |              |--- Index
       |---- BackEnd
               |--- Controllers
               |      |---- HomeController
               |--- Views
                      |--- Index
Run Code Online (Sandbox Code Playgroud)

Update1 要启动区域中的特定控制器,我试过这个:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "Default",
        "{controller}/{action}/{id}",
        new { controller = "Home", action = "Index", id = UrlParameter.Optional },
        new[] { "MyProject.Areas.BackEnd.Controllers" }
    );
}
Run Code Online (Sandbox Code Playgroud)

Dar*_*rov 15

请尝试以下方法:

~/Global.asax.cs:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "Default",
        "{controller}/{action}/{id}",
        new { controller = "Home", action = "Index", id = UrlParameter.Optional },
        new[] { "MyProject.Controllers" }
    );
}
Run Code Online (Sandbox Code Playgroud)

~/Areas/Administration/AdministrationAreaRegistration.cs

public override void RegisterArea(AreaRegistrationContext context)
{
    context.MapRoute(
        "Administration_default",
        "Administration/{controller}/{action}/{id}",
        new { action = "Index", id = UrlParameter.Optional },
        new[] { "MyProject.Areas.Administration.Controllers" }
    );
}
Run Code Online (Sandbox Code Playgroud)

~/Areas/FrontEnd/FrontEndAreaRegistration.cs:

public override void RegisterArea(AreaRegistrationContext context)
{
    context.MapRoute(
        "FrontEnd_default",
        "FrontEnd/{controller}/{action}/{id}",
        new { action = "Index", id = UrlParameter.Optional },
        new[] { "MyProject.Areas.FrontEnd.Controllers" }
    );
}
Run Code Online (Sandbox Code Playgroud)

现在,当您请求时/Administration/Home/Index,将调用该区域中的Index操作,它将查找该视图.确保此视图出现在此位置.在你的图片中你似乎省略了目录 - .HomeControllerAdministration~/Areas/Administration/Views/Home/Index.cshtmlHome~/Areas/Administration/Views/Index.cshtml