I would like to get the current URL and append an additional parameter to the url (for example ?id=1)
I have 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 doesnt 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?
From stackoverflow
-
The order of the rules makes sence. Try to insert this rule as first.
Also dont forget to define constraints if needed - it will results in better rule matching:
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 ); -
I added the constraints to my rule as you suggested, however, urlHelper.RouteUrl still generates a URL with the year, month and date as a query string i.e.
gig/list/?year=2008&month=11&day=01&id=1
rather than the expected:
gig/list/2008/11/01?id=1
:(
maxnk : try to add this rule before any other rules, I've just tried and it works. If its not possible to make this rule first in the list of rules, you can try call RouteUrl as RouteUrl("GigDayListings", _helper.ViewContext.RouteData.Values)
0 comments:
Post a Comment