1

2つの領域と1つのルート領域を持つMVC3アプリケーションがあります。一般的な構造はルートのように見えます

- Root/Areas/Manager
    * Views/Home/Index.cshtml
    * ManagerAreaRegistration.cs
- Root/Areas/Participant
    * Views/Home/Index.cshtml
    * ParticipantAreaRegistraion.cs
- Root
    * Views/Home/Index.cshtml
    * Views/Account/Register.cshtml
    * Global.asax.cs

ルーティングに関して2つの問題があります。1つ目は、Global.asax.csファイルでデフォルトとして設定されているページを除いて、Root/Viewsフォルダー内のどのページにも移動できないことです。Global.asax.csファイルは次のようになります。

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
   "Default", // Route name
   "{controller}/{action}/{id}", // URL with parameters
   new {controller="Home" , action = "Index", id = UrlParameter.Optional }, 
   new[] { "MVCApplication.Controllers" }    // for areas 
   );
...     
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
...

また、スタートページであるRoot / Views / Home/Index.cshtmlのコードは次のようになります。

@Html.ActionLink("Accounts","Register", new {area="", controller="Accounts"},null)
@Html.ActionLink("Manager", "Index", new { area = "Manager", controller = "Home" })
@Html.ActionLink("Participant", "Index", new { area = "Participant", controller = "Home" })

各エリアの登録ファイルにルートを追加したため、2つのエリアリンクは正常に機能しますが、ルートの別のページであるアカウント/登録へのリンクで「リソースが見つかりませんエラー」が発生します。ただし、Global.asax.csルートを次のように変更すると

    new {controller="Accounts" , action = "Register", id = UrlParameter.Optional }, 

デフォルトルートでは、登録ページから始めることができます。

だから私の最初の質問は:エリアとルートの両方のページ(つまり、アカウント/登録ページ)にアクセスできるようにルートを使用するにはどうすればよいですか?

私の2番目の質問は、地域自体に関係しています。どちらにも「ホーム」コントローラーがあるので、区別するためにエリア名を前に付けましたが、これは必要ありません。現在、'ParticipantAreaRegistration.csファイルのコードは次のとおりです。

  context.MapRoute(
            "Participant_default",
            "Participant/{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional },
          new[] { "MvcApplication.Areas.Participant.Controllers"  }    // for areas 
            );

これは「localhost**/ Participant/Home」のURLを提供ます

ManagerAreaRegistraion.csにはコードがあります

 context.MapRoute(
            "Manager_default",
            "{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional },
                         new[] { "MvcApplication.Areas.Manager.Controllers" }    // for areas 

        );

これは「localhost***/Home/」のURLを提供します

2番目の質問は次のとおりです。URLにエリア名を表示せずに、マネージャーと参加者の両方(または任意の数のエリア)の「localhost ** / Home」のURLを取得するにはどうすればよいですか?

この質問はすでに登録されている他の質問と似ていることは知っていますが、私はこれらを無駄に精査し、現在非効率に溺れているので、具体的に質問してみようと思いました。ありがとう。

4

1 に答える 1

0

カスタムルーティングを使用できます。

これに似たもの:

MVC 2 AreaRegistration Routes Order

上記の問題の解決策を使用して、ルーティングのカスタム順序を記述できます。

私のアプリケーションの1つに、Admin、Blog、Members、Publicという名前の領域があります。パブリックエリアをURLとしてルーティングしました: http:// localhost:4000 /、管理者として:http:// localhost:4000 / admin、ブログとして:http:// localhost:4000/blogなど。私のコードが欲しい、私はあなたに与えることができます。

于 2012-01-24T10:39:01.283 に答える