2012-11-19 127 views
26

我創建了具有3個不同區域的MVC應用程序。 (管理員,用戶,新聞) 這是我RouteConfig.cs文件中App_Start目錄:如何註冊路由區域

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

     routes.MapRoute(
      name: "Default", 
      url: "{controller}/{action}/{id}", 
      defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }, 
      namespaces: new[] { "TestMvcApplication.Controllers" } 
     ); 
    } 
} 

,這是我AdminAreaRegisteration.cs文件:

namespace TestMvcApplication.Areas.Admin 
{ 
    public class AdminAreaRegistration : AreaRegistration 
    { 
     public override string AreaName 
     { 
      get 
      { 
       return "Admin"; 
      } 
     } 

     public override void RegisterArea(AreaRegistrationContext context) 
     { 
      context.MapRoute(
       "Admin_default", 
       "Admin/{controller}/{action}/{id}", 
       new { controller = "Home", action = "Index", id = UrlParameter.Optional }, 
       namespaces: new[] { "TestMvcApplication.Areas.Admin.Controllers" }     
      ); 
     } 
    } 
} 

最後,這是我的Global.asax .cs文件內容:

namespace TestMvcApplication 
{ 
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801 

    public class MvcApplication : System.Web.HttpApplication 
    { 
     protected void Application_Start() 
     { 
      AreaRegistration.RegisterAllAreas(); 

      WebApiConfig.Register(GlobalConfiguration.Configuration); 
      FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 
      RouteConfig.RegisterRoutes(RouteTable.Routes); 
      BundleConfig.RegisterBundles(BundleTable.Bundles); 
      AuthConfig.RegisterAuth(); 
     } 
    } 
} 

我的網站主頁完全加載,它的工作原理。但管理員或其他地區的主頁不被檢測的路線,我給這個錯誤信息:

Server Error in '/' Application. 
The resource cannot be found. 
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly. 

Requested URL: /Admin/Home 

我怎樣才能解決這個問題? 謝謝。

+1

實際上你的管理員區域是否有家庭控制器? – James

+0

是的,我有一個每個區域的HomeController.cs類。 – Mojtaba

+0

你的HomeController是否有索引方法?你是否重寫了'AreaName'屬性? – James

回答

26

呼叫AreaRegistration.RegisterAllAreas()某處你RegisterRoutes

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

提示:使用像RouteDebugger 2.0或​​的工具來調查你的路由

獲取最新的NuGet: Route Debugger for MVCRouteDebugger for WepApi

這裏有一個教程How to set up and use RouteDebugger with WebApi

+1

此代碼位於我的Global.asax.cs文件中,Visual Studio創建了這些方法。 – Mojtaba

+1

路由調試器怎麼樣?你能發佈結果嗎? –

+0

我使用了路由調試器,但無法加載頁面,問題仍然存在。路由調試器僅顯示已加載頁面中的路由。 – Mojtaba

18

從提供的代碼,我可以看到2點潛在的問題:

  1. 你是不是叫RegisterAllAreas
  2. 你似乎並沒有被重寫AreaName財產

試着改變你代碼爲:

Global.asax

public static void RegisterRoutes(RouteCollection routes) 
{ 
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 
    AreaRegistration.RegisterAllAreas(); 
    routes.MapRoute(
     name: "Default", 
     url: "{controller}/{action}/{id}", 
     defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }, 
     namespaces: new[] { "TestMvcApplication.Controllers" } 
    ); 
} 

管理區

public class AdminAreaRegistration : AreaRegistration 
{ 
    public override string AreaName 
    { 
     get 
     { 
      return "Admin"; 
     } 
    } 

    public override void RegisterArea(AreaRegistrationContext context) 
    { 
     context.MapRoute(
      "Admin_default", 
      "Admin/{controller}/{action}/{id}", 
      new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
     ); 
    } 
} 
4

只需創建一個靜態類命名爲AreaConfig用靜態方法RegisterAreas()這裏代碼:

public static class AreaConfig 
{ 
    public static void RegisterAreas() 
    { 
     // 
     // Admin area . . . 

     var adminArea = new AdminAreaRegistration(); 
     var adminAreaContext = new AreaRegistrationContext(adminArea.AreaName, RouteTable.Routes); 
     adminArea.RegisterArea(adminAreaContext); 


     // 
     // Default area . . . 

     var defaultArea = new DefaultAreaRegistration(); 
     var defaultAreaContext = new AreaRegistrationContext(defaultArea.AreaName, RouteTable.Routes); 
     defaultArea.RegisterArea(defaultAreaContext); 
    } 
} 

然後調用它像這樣的Global.asax.cs文件:

protected void Application_Start() 
    { 
     . . . 

     AreaConfig.RegisterAreas(); 

     . . . 
    }