How to match Home / Action / id with action / id?

At the moment I only have this route defined

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

I want to map / Home / Action 1 / id to just / Action1 / id does anyone know how?

+4
source share
3 answers

You may have to be careful with the route as you ask, because it can catch more than intended.

One way is to restrict this route with restrictions, if possible.

eg. Verify that the identifier is numeric ...

 routes.MapRoute("ActionRoute", "{action}/{id}", new { controller = "Home", action = "Index", id="1" }, new { id = @"\d{1,6}" } ); 

Thus, you can indicate your default common route at the end to catch other routes on the site, if necessary.

+3
source

look at this question How to get directions / About us / Home / About the program

This is not an (accurate) answer, but I think it gives the answer in reverse order.

+2
source
  routes.MapRoute(        "NewRoute",                                "{id}",                     new { controller = "Home", action = "Index", id = "" }        ); 

try, if you use the controller by default and the action should work. Do not forget when generatig contacts Html.Action to use the new route name.

Apologies for formatting written on my iPhone

+2
source

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


All Articles