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を取得するにはどうすればよいですか?
この質問はすでに登録されている他の質問と似ていることは知っていますが、私はこれらを無駄に精査し、現在非効率に溺れているので、具体的に質問してみようと思いました。ありがとう。