2012-02-04 97 views
5

在具有区域的ASP.NET MVC 3应用程序中(请参阅下面的架构)。根目录下的“控制器”目录已被删除。不同区域中的相同控制器名称

当我这样做:

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

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

回答

11

尝试以下操作:

~/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/IndexHomeControllerAdministration面积Index动作将被调用,它会寻找~/Areas/Administration/Views/Home/Index.cshtml视图。确保此视图存在于此位置。在你的照片中,你似乎省略了Home目录 - ~/Areas/Administration/Views/Index.cshtml

+0

看起来没问题。还有一个想法。当我在VS中打F5时,我想默认去Home/Index – 2012-02-05 08:23:18

+0

@ Kris-I的Backend区域,在这种情况下,可以修改你的区域路径注册或去你的项目的属性,并在Web选项卡中可以设置一个启动页面,您可以在其中定义“管理/主页/索引”。这种方式当你运行应用程序时,它会自动导航到这个控制器。 – 2012-02-05 08:30:49

+0

更改启动页面正常,但通过路由注册查看update1。谢谢 – 2012-02-05 09:09:27