Asp.NET MVC 2 Routing

I want to define a route with 2 optional parameters in the middle of the URL start end - digits

routes.MapRoute( "", "Source/Changeset/{start}/{end}/{*path}", new { controller = "Source", action = "Changeset", start = UrlParameter.Optional, end = UrlParameter.Optional, path = "crl" }, new { start = @"\d+", end = @"\d+" } ); 

I tried different approaches and none of them worked, I could use some help.

Thanks in advance.

EDIT

I manage to solve the problem this way, but it is far from elegant.

 routes.MapRoute( "", "Source/Changeset/{start}/{end}/{*path}", new { controller = "Source", action = "Changeset", start = UrlParameter.Optional, end = UrlParameter.Optional, path = "crl" }, new { start = @"\d+", end = @"\d+" } ); routes.MapRoute( "", "Source/Changeset/{start}/{*path}", new { controller = "Source", action = "Changeset", start = UrlParameter.Optional, path = "crl" }, new { start = @"\d+" } ); routes.MapRoute( "", "Source/Changeset/{*path}", new { controller = "Source", action = "Changeset", path = "crl" } ); 
+4
source share
1 answer

you will need routes for all possible combinations that can get a little hairy:

 //route if everything is included routes.MapRoute(null, ""Source/Changeset/{start}/{end}/{*path}", new { controller = "Home", action = "Index" } ); //route if nothing is included routes.MapRoute(null, ""Source/Changeset/{*path}", new { controller = "Home", action = "Index", start=0, end=5 } //defaults ); //and so on... 

But there is a problem here: since the beginning and the end are two digits, there would be no way (since they are optional) to decide whether there will be a β€œ2” in Source / Changeset / 2 / fodass - this is the starting or ending variable, so you may have to come up with a new approach or change your value to something like Source / Changeset / Start / 2 / fodass , Source / Changeset / End / 5 / fodass , Source / Changeset / Start / 2 / End / 5 / fodass etc.

0
source

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


All Articles