开发者

.NET MVC custom routing

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜