MapPageRoute additional parameters in URL

asp.net, url-routing

Solution

If what you want is to pass additional parameters to your route handler(page), you can set DataTokens property,

Route reportRoute = new Route("Accessoires/{accessory_name}", new PageRouteHandler("~/Accessory.aspx"));
reportRoute.DataTokens = new RouteValueDictionary { { "lang", "en" } };
routes.Add(reportRoute);

You could access this in your handler(page),

string lang = (string)Page.RouteData.DataTokens["lang"];

Check msdn, Route.DataTokens Property

Problem

I'm using MapPageRoute (ASP.NET 4) for supporting friendly urls in a multi language web site. I want to be able to pass additional parameters along with the ones defined in the MapPageRoute. In the example above, I need to determine the requested language according to the URL: (if it is "Accessories", the page should be in English, if it is "Accessoires" - the page should be in French): ``` routes.MapPageRoute( "Accessory fr", "Accessoires/{accessory_name}", "~/Accessory.aspx?lang=fr" ); routes.MapPageRoute( "Accessory en", "Accessories/{accessory_name}", "~/Accessory.aspx?lang=en" ); ``` So, no problem with the requested accessory name, it is read by using `Page.RouteData.Values["accessory_name"]`, but how can I read the `lang` parameter ? I cannot use `Request["lang"]` since it doesn't give me the requested results. Somehow, the `?lang=xx` is not taken . Any other idea, how can I pass additional parameter, assuming this parameter is not found explicitly in the URL ?

Original source