asp.net mvc 2 How to get the first part of the url parameters and pass it to controllers?
I'm building a site that has a number of language versions. currently I use session variable to manage the country code for different language version
but today the client specifically requires to put the country code in the url right behind the domain name so the site url will be like these examples
uk version: www.mysite.com/uk/{controller}/{action}/{id}
usa version: www.mysite.com/usa/{controller}/{action}/{id}
I defined the default route to
routes.MapRoute(
"Default", // Route name
"{country}/{controller}/{action}/{id}", // URL with parameters
new { country ="uk", controller = "Home", action 开发者_JAVA技巧= "Index", id = UrlParameter.Optional } // Parameter defaults.uk is the default value
);
but I'm not sure how to get the first segment of the url in the controllers?
Found the solution, Just use RouteData.Values["country"], nice and simple!
精彩评论