.NET MVC自定义路由 [英] .NET MVC custom routing
问题描述
我想知道如果我可以创建一个路由图比控制器多了一个更高的水平。典型的路由将包括/控制器/操作/ ID。我所寻找的是类似一节/控制器/操作/ ID或控制器/节/动作/ ID。我怎样才能做到这一点?
I was wondering if I could create a routing map with one more higher level than the controller. The typical routing would include "/controller/action/id". What I am looking for is something like "section/controller/action/id" or "controller/section/action/id". How can i do this?
推荐答案
没有问题。只要创建一个路由它的URL,例如
No problem. Just create a route the URL of which is, for example
路径/要/我的/应用/ {控制器} / {行动} / {ID}
...并提供一个默认的控制器和行动如常。
...and supply a default controller and action as usual.
这方面的一个具体的例子是
A concrete example of this is
context.MapRoute(
"Admin_default",
"admin/{controller}/{action}/{id}",
new { controller = "AdminHome", action = "Index", id = "" }
);
这将映射,例如,以下网址:
This will map, for example, the following URLs:
/admin/ => AdminHomeController.Index
/admin/adminhome/ => AdminHomeController.Index
/admin/other/ => OtherController.Index
/admin/statistics/view/50 => StatisticsController.View(50)
请注意,虽然,如果你也有一个默认路由,例如像这样的:
Note, though, that if you also have a default route, for example like this:
context.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" }
);
...然后在管理路由控制器的操作方法也可通过这条路线到达。使用 URL路由调试找出肯定的。
这篇关于.NET MVC自定义路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!