Passing a
querystring to a named route in
ASP.NET MVC is not well documented.
I stumbled across this by mistake! Using either the
Html.RouteLink extention or the
RedirectToRoute method it is possible to pass any number of
querystring parameters to the route.
All you have to do is pass the parameter in the
RouteValueDictionary. For it to appear on the
querystring the parameter name must not appear on the named route in the
RouteCollection.
This is easier to explain in code!
Route (in
Global.ascx.cs):
routes.MapRoute(
"Default",
"{controller}/{action}/{id}"
);
using Html.RouteLink:
<%= Html.RouteLink("Click Me!", "Default", new { controller = "Home", action = "Index", id = "1234", myparameter = "hello" }) %>
using RedirectToRoute:
return RedirectToRoute("Default", new {controller = "Home", action = "Index", id = "1234", myparameter = "hello"});
The above both direct to:
http://www.mywebsite.com/Home/Index/1234?myparameter=hello