Having this
routes.MapRoute(
"ShowPage",
"default.aspx/{page}/{pagetype}",
new {
controller = "Info",
action = "PageASPX",
page = "emptypage",
pagetype = "emptypagetype"
}
);
To catch default.aspx?page=order
(I need to generate a permanet redirect fom the old site to my new MVC site)
The route is matched as I can see in route debugger, but I don’t get any values in the route
Key Value
page emptypage
pagetype emptypagetype
controller Info
action PageASPX
What’s wrong??
MVC route definitions have URL segments. Your route looks like this:
this means that requests like
default.aspx/ordershould populatepagevalue during route resolution because it’s provided as a URL segment. This doesn’t mean thatpagewon’t get populated later on when model binding occurs and your controller action gets invoked.If your controller action signature seems similar to this:
Those two parameters may still get populated even when you’d provide them as query variables and not URL segments. The only requirement is that routing doesn’t provide default values for missing segment values (or they’re set as optional). Default MVC model binder will then populate
pageaction parameter with actual query variable value.So the main thing is that you should change your routing definition (see last section of my answer).
Requests that should work
In order for your routing to recognise
pageandpagetypeURL segment variables your requests should look like this:Provided that your routing doesn’t define defaults for
pageandpagetypethen your controller actions will getpageandpagetypeparameters populated with correct values when your requests look like:Mixing route values and query string variables
As my tests show Asp.net MVC doesn’t provide route value overriding when it has a value (either provided in the URL itself as segment or as route defaults). In your case if you’d request this URL:
your action method would see
pageparameter to have value of routepage. Always.The more important aspect of this is that when you provide default value in route definition itself, you won’t be able to set it as a query string at all even when you omit it from the URL. To avoid this issue, you have two choices:
set
pageandpagetypeasUrlParameter.Optionalwhich will allow you to override their values with query string variables when URL won’t have them – this means that you can always use only URL segments or query strings, but not both, because URL segments prevailshave different names for URL segment variables and query string variables – the downside is that you’d need to have your action with double parameters which is undesired:
Best solution
Change your routing so you have separate routing definition for variables provided as URL segments and another for variables provided as query string variables. Then wire these routes to the same controller action as long as all variables (in segments and query string) share the same names:
If your requests provide both values (segments and query variables) as in
then segment values have precedence so action parameter
pagewill have value of segmentpage for this request.