Creating URLs using asp.net MVC and RouteUrl

I would like to get the current url and add an extra parameter to the url (e.g. id = 1)

I defined a route:

routes.MapRoute( "GigDayListings", // Route name "gig/list/{year}/{month}/{day}", // URL with parameters new { controller = "Gig", action = "List" } // Parameter defaults ); In my view I have a helper that executes the following code: // Add page index _helper.ViewContext.RouteData.Values["id"] = 1; // Return link var urlHelper = new UrlHelper(_helper.ViewContext); return urlHelper.RouteUrl( _helper.ViewContext.RouteData.Values); 

However, this does not work.

If my original URL was: gig / list / 2008/11/01

I get

gig / list / year = 2008 &? Month = 11 & day = 01 & ID = 1

I would like the url to be: controller / action / 2008/11/01? ID = 1

What am I doing wrong?

+3
source share
1 answer

The order of the rules. Try to insert this rule first.

Also, do not forget to define the restrictions if necessary - this will lead to a better match of the rules:

 routes.MapRoute( "GigDayListings", // Route name "gig/list/{year}/{month}/{day}", // URL with parameters new { controller = "Gig", action = "List" }, // Parameter defaults new { year = @"^[0-9]+$", month = @"^[0-9]+$", day = @"^[0-9]+$" } // Constraints ); 
+1
source

All Articles