How to get there / About us / Home / About company

I'm just getting started with ASP.NET MVC, and that's great! However, I do not quite understand how to configure the routes.

How do I route ~ / About ~ / Home / About?

/Views/Home/About.aspx

I would like to have access to it using / Home / O or just / O

+2
source share
2 answers

If you want it to be configured for routing, you can do something like this:

routes.MapRoute( "AboutRoute", "About", new { controller = "Home", action = "About" } // Parameter defaults ); 

I think you want to do? That is, have / About is handled by the home controller?

The default route (as shown below) processes / Home / About

  routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = "" } // Parameter defaults ); 
+7
source

In response to your comment on RM's answer - you really don't need wildcards. Just do

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

Please note, however, that you need to put this route at the very end of the route table (and you will need to delete the default route), as this will catch every URL that enters.

You can use Phil Haack Route Debugger to make sure your routes collect URLs as you expect.

+7
source

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


All Articles