How to configure a URL with three levels in ASP.NET MVC?

Using ASP.NET MVC, I need to configure my URLs as follows:

www.foo.com/company: render View Company

www.foo.com/company/about: render View Company

www.foo.com/company/about/mission: visualize View Mission

If the "company" is my controller, and the "o" is my action, what should be the "mission"?

For each β€œfolder” (company, about mission and mission) I have to display a different view.

Does anyone know how I can do this?

Thanks!

+4
source share
1 answer

First set up your views:

Views\ Company\ Index.aspx About.aspx Mission.aspx AnotherAction.aspx 

In your GlobalAsax.RegisterRoutes (RouteCollection) routes:

 public static void RegisterRoutes(RouteCollection routes) { // this will match urls starting with company/about, and then will call the particular // action (if it exists) routes.MapRoute("mission", "company/about/{action}", new { controller = "Company"}); // the default route goes at the end... routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = "" } // Parameter defaults ); } 

In the controller:

 CompanyController { public ViewResult Index() { return View(); } public ViewResult About() { return View(); } public ViewResult Mission() { return View(); } public ViewResult AnotherAction() { return View(); } } 
+4
source

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


All Articles