Custom .NET MVC Routing

I was wondering if I can create a routing map with an even higher level than the controller. Typical routing will include "/ controller / action / id". I am looking for something like section / controller / action / id or "controller / section / action / id". How can i do this?

+3
source share
1 answer

No problems. Just create a route whose URL is, for example

path/to/my/application/{controller}/{action}/{id}

... and set the 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 display, for example, the following URLs:

/admin/                   => AdminHomeController.Index
/admin/adminhome/         => AdminHomeController.Index
/admin/other/             => OtherController.Index
/admin/statistics/view/50 => StatisticsController.View(50)

Please note that if you also have a default route, for example, for example:

context.MapRoute(
    "Default",
    "{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = "" }
);

... . URL- , .

+4

Source: https://habr.com/ru/post/1721061/


All Articles