views:

19

answers:

1

I have a route added by the code

routes.MapRoute("MyRoute", "TheUrl", new { controller = "MyController", action = "MyAction" });

I can then do a reverse lookup with the arguments like UrlHelper.Action("MyAction", "MyController"), and it will return a nice url like ~/TheUrl

However, for this route I want the generated URL to be ~/TheUrl?p=2354, with the parameter being some versioning parameter. Is there a way of doing this by mapping the route with some customized route handler or something? The versioning parameter will be non-standard and require some custom code to execute every time the Url is looked up.

A: 

I think a UrlHelper extension method would be most ideal and simple here specially.

public string MyRoute(this UrlHelper url)
{
     string versionNumber = GetVersionNumber();  // or w/e is required to get it
     return Url.Action("MyAction", "MyController") + "?p=" + versionNumber;
}   

This would make calling that route much easier in html

<%= Url.MyRoute() %>
Baddie
Yes, but what if someone then uses Html.ActionLink() to render the Url?
erikkallen
You could implement another extension method to Html for that specific route. I don't know of a way to directly edit any Urls generated by Html.ActionLink or Url.Action for a specific route configuration.
Baddie