Reputation: 13308
There is an url
http://mysite.com/en-us/articles/browse/en-us/12/byCategory
It's known that one of these parameters named culture
. Of course I can call ViewContext.RouteData.Values["culture"]
to get the value of the culture
, but it doesn't say me which en-us
parameter (first or second) named culture
.
My task is recognize which of parameters named culture
, replace it with specified value and return the new url. How to do it?
Upvotes: 1
Views: 1192
Reputation: 105039
So if you say ViewContext.RouteData.Values["culture"]
you will get the correct culture value that is related to a specific segment either the first or the second one, depending on your route URL definition which may look like:
{culture}/{controller}/{action}/{lang}/{id}/{filter}
As you can see there's only one segment parameter called culture
.
Note: upper route URL definition may not be like yours but it shows an example that could work with your request URL.
But otherwise you could get the currently executing route URL definition to locate the correct segment by:
((Route)ViewContext.RouteData.Route).Url
Casting RouteBase
to Route
would probably work in 99% of cases because using custom route classes isn't that common, but even though custom classes are more likely to inherit from Route
than RouteBase
.
Parsing this URL definition may be a more challenging task depending on the complexity of your route definitions. But you could more or less successfully match any URL definition against request URL segments. And this may be exactly what you're after.
But instead of parsing route's URL and replacing correct segment, you could let route itself do it for you simply by:
((Route)ViewContext.RouteData.Route)
.GetVirtualPath(
ViewContext.RequestContext,
new RouteValueDictionary(new { culture = "en-GB" }))
.VirtualPath
Upvotes: 3