2012-02-04 118 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

相關問題