.NET MVCカスタムルーティング
-
06-07-2019 - |
質問
コントローラーよりも1つ高いレベルのルーティングマップを作成できるかどうか疑問に思っていました。典型的なルーティングには、「/ controller / action / id」が含まれます。私が探しているのは、「section / controller / action / id」のようなものです。または「コントローラ/セクション/アクション/ ID」。どうすればいいですか?
解決
問題ありません。たとえば、URLが次のようなルートを作成します。
path / to / my / application / {controller} / {action} / {id}
...そして通常のデフォルトのコントローラーとアクションを提供します。
この具体例は
ですcontext.MapRoute(
"Admin_default",
"admin/{controller}/{action}/{id}",
new { controller = "AdminHome", action = "Index", id = "" }
);
これにより、たとえば次のURLがマッピングされます。
/admin/ => AdminHomeController.Index
/admin/adminhome/ => AdminHomeController.Index
/admin/other/ => OtherController.Index
/admin/statistics/view/50 => StatisticsController.View(50)
ただし、デフォルトルートもある場合、たとえば次のようになります。
context.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" }
);
...管理ルーティングのコントローラーアクションメソッドにも、このルートを介してアクセスできます。 URL Routing Debugger を使用して確認してください。 。
所属していません StackOverflow