.NET MVC benutzerdefinierte Routing
-
06-07-2019 - |
Frage
Ich habe mich gefragt, ob ich eine Routing-Karte mit einem weiteren höheren Niveau als der Controller schaffen könnte. Das typische Routing würde "/ controller / action / id". Was ich suche ist so etwas wie "Abschnitt / controller / action / id" oder "Controller / section / action / id". Wie kann ich das tun?
Lösung
Kein Problem. Erstellen Sie einfach eine Route die URL, von denen zum Beispiel
path/to/my/application/{controller}/{action}/{id}
... und einen Standard-Controller und die Aktion wie gewohnt liefern.
Ein konkretes Beispiel hierfür ist
context.MapRoute(
"Admin_default",
"admin/{controller}/{action}/{id}",
new { controller = "AdminHome", action = "Index", id = "" }
);
Dies wird Karte, beispielsweise die folgenden URLs:
/admin/ => AdminHomeController.Index
/admin/adminhome/ => AdminHomeController.Index
/admin/other/ => OtherController.Index
/admin/statistics/view/50 => StatisticsController.View(50)
Beachten Sie jedoch, dass, wenn Sie auch eine Standardroute haben, zum Beispiel wie folgt aus:
context.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" }
);
... dann Controller-Aktion Methoden in dem Admin-Routing können auch über diesen Weg zugänglich sein. Verwenden Sie die URL Routing Debugger sicher, um herauszufinden, .