2016-10-21 16 views
0

私は名前Adminによって面積を有し、かつAdminAreaRegistrationに私は打撃のようなルートを定義します。複数のルート登録

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 } 
     ); 
     context.MapRoute(
     name: "Product", 
      url: "Admin/ProductForm-{FormName}", 
     defaults: new { controller = "ProductForm", action = "Index", id = UrlParameter.Optional } 
    ); 
    } 
} 

ProductFormControllerに(Admin領域に)私が持っている:

public ActionResult Index(string FormName) 
    { 
     return View(); 
    } 

私はこのURLに行きたい:http://localhost:5858/Admin/ProductForm-mobile、それは(フォーム名=モバイルで)ProductFormControllerIndexアクションにルーティングする必要がありますが、それはしませんでした。何が問題ですか?

+0

? –

+0

@CristianSzpisjakは '404'エラーを返します – pejman

+0

' Admin_defaut'ルートを最後に置きます。デフォルトルートは常に最後のルートにする必要があります。 –

答えて

1

ASP.NET MVCでは、ルートメカニズムは非常に簡単な方法で動作します。最初の一致では、そのパターンにリダイレクトされ、次のものは無視されます。そのため、Default Routeは常に最後のルートにする必要があります。

"Admin_defaut"ルートを最後まで移動する必要があります。

public override void RegisterArea(AreaRegistrationContext context) 
{ 
    context.MapRoute(
     "Admin_default", 
     "Admin/{controller}/{action}/{id}", 
     new {Controller="Home", action = "Index", id = UrlParameter.Optional } 
    ); 
    context.MapRoute(
    name: "Product", 
     url: "Admin/ProductForm-{FormName}", 
    defaults: new { controller = "ProductForm", action = "Index", id = UrlParameter.Optional } 
    ); 
} 

正しい実装:それはにリダイレクト

public override void RegisterArea(AreaRegistrationContext context) 
{ 
    context.MapRoute(
     name: "Product", 
     url: "Admin/ProductForm-{FormName}", 
     defaults: new { controller = "ProductForm", action = "Index", id = UrlParameter.Optional } 
    ); 

    context.MapRoute(
     "Admin_default", 
     "Admin/{controller}/{action}/{id}", 
     new {Controller="Home", action = "Index", id = UrlParameter.Optional } 
    ); 
} 
関連する問題