【问题标题】:Same controllername in differents areas不同区域的相同控制器名称
【发布时间】:2012-02-04 22:52:30
【问题描述】:

在带有区域的 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”的重载来注册此路由 采用“命名空间”参数的方法。 '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" }
    );

我得到 tis 错误:

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

更新1 要开始使用区域中的特定控制器,我尝试了这个:

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

【问题讨论】:

    标签: asp.net-mvc


    【解决方案1】:

    尝试以下方法:

    ~/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 时,HomeControllerAdministration 区域中的Index 操作将被调用,它将查找~/Areas/Administration/Views/Home/Index.cshtml 视图。确保此视图出现在此位置。在您的图片中,您似乎省略了 Home 目录 - ~/Areas/Administration/Views/Index.cshtml

    【讨论】:

    • 看起来不错。再想一想。当我在 VS 中按 F5 时,我想默认转到 Home/Index 中的后端区域
    • @Kris-I,在这种情况下,要么修改您的区域路线注册,要么转到您项目的属性,然后在 Web 选项卡中设置一个启动页面,您可以在其中定义Administration/Home/Index。这样,当您运行应用程序时,它会自动导航到该控制器。
    • 更改启动页面ok,但是通过路由注册看看update1。谢谢
    • @Kris-I,你为什么要修改你的 Global.asax?您应该修改 AdministrationAreaRegistration。您是否看到其中所有路由都必须以 Administration 前缀开头?如果您不进行更改,您将无法自动将诸如/ 之类的请求路由到/Administration/Home/Index。此外,我认为您不会有诸如 / 之类的请求映射到某个区域,除非您使用自定义视图引擎:stackoverflow.com/questions/2140208/…
    • 这不是你的代码错误吗?您在第二个代码中更改 AdministrationAreaRegistration.cs 它是 FrontEnd 但您编写 MyProject.Areas.Administration.Controllers
    猜你喜欢
    • 1970-01-01
    • 2020-02-13
    • 1970-01-01
    • 2015-06-20
    • 1970-01-01
    • 2011-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多