Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top