开发者

.NET MVC custom routing

开发者 https://www.devze.com 2022-12-09 16:16 出处:网络
I was wondering if I could create a routing m开发者_开发知识库ap with one more higher level than the controller. The typical routing would include \"/controller/action/id\". What I am looking for is s

I was wondering if I could create a routing m开发者_开发知识库ap 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?


No problem. Just create a route the URL of which is, for example

path/to/my/application/{controller}/{action}/{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 = "" }
);

...then controller action methods in the Admin routing may also be accessible via this route. Use the URL Routing Debugger to find out for sure.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号