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

时间:2012-02-04 22:52:31

标签: 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
    );

我收到此错误: 发现多个类型与名为“Home”的控制器匹配。 如果为此请求提供服务的路由('{controller} / {action} / {id}'),则会发生这种情况 不指定名称空间来搜索与请求匹配的控制器。 如果是这种情况,请通过调用'MapRoute'的重载来注册此路由 采用'namespaces'参数的方法。 对'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" }
    );

我得到错误:

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

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

1 个答案:

答案 0 :(得分:13)

尝试以下方法:

~/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" }
    );
}

~/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" }
    );
}

~/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" }
    );
}

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