2013-03-25 5 views
13

エリアのフォルダは次のようになります。Mvcエリアルーティング?

Controller 
    UserController 
     GetAllUsers 

エリアルート登録

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

プロジェクトルート登録

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 string[] { "MyApp.Areas.Admin.Controllers" }); 
} 

Areas 
    Admin 
     Controllers 
      UserController 
      BranchController 
      AdminHomeController 

プロジェクトディレクトリは次のようになり3210

私がこのように経路を設定したとき:http://mydomain.com/User/GetAllUsersリソースが見つかりません。エラー(404)が表示されます。 UserControllerをAreaに追加した後にこのエラーが発生します。

このエラーを修正するにはどうすればよいですか?あなたは、コントローラの名前空間を台無しにしました

答えて

26

おかげで...。

あなたの主なルート定義は次のようになります。

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

そして、あなたの管理エリアルート登録をする必要があります:正しい名前空間を使用すべきか

public override void RegisterArea(AreaRegistrationContext context) 
{ 
    context.MapRoute(
     "Admin_default", 
     "Admin/{controller}/{action}/{id}", 
     new { action = "Index", id = UrlParameter.Optional }, 
     new { controller = "Branch|AdminHome|User" }, 
     new[] { "MyApp.Areas.Admin.Controllers" } 
    ); 
} 

注意してください。

+0

私はこの分野に対応する名前空間に混乱しています。この例では、名前空間MyApp.Areas.Admin.Controllersはフォルダ階層と一致しますが、名前空間の定義は任意です。つまり、プログラマが望むコントローラクラスに名前空間を割り当てることができるということです。または、フォルダ階層に一致させるために名前空間を必要とするいくつかのasp.net mvc規則がありますか? – Howiecamp

+1

@ Howiecamp Visual Studioのデフォルトの動作は、ネームスペースをフォルダ階層に一致させることです。これは、通常はすべての.netプロジェクト(MVCプロジェクトだけでなく)に表示されます。 –