How to use routing in a ASP MVC website to localize in two languages - But keeping exiting URLs
We have a couple ASP MVC websites just using the standard VS templates default settings - Working as wanted. But now I want to localize these website ( They are now in Dutch and I will add the English language ) I would like to use routing and not Resource because: 1. Languages will differ in content, numbers of pages, etc. 2. The content is mostly text.
I would like the URLs to look some thing like this - www.domain.com/en/Home/Index, www.domain.nl/nl/Home/Index. But the last one should also work with - www.domain.nl/Home/Index - Witch is the exciting URLs.
I have implemented Phil Haacks areas ViewEngine from this blogpost - http://haacked.com/archive/2008/11/04/areas-in-aspnetmvc.aspx. But only putting the English website in the areas and keeping the Dutch in old structure. Witch are served as Phils default fallback. But the problem is here that I have to duplicate my controllers for both language's.
So I tried the work method described in this tread - ASP.NET MVC - Localization route. It works OK with the ?en? and /nl/ but not with the old URLs.
When using this code in the global.asax the URL without the culture isn't working.
public static void RegisterRoutes(RouteCollection routes)
{
//routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{culture}/{controller}/{action}/{id}", // URL with parameters
new { culture = "nl-NL", controller = "Home", action = "Index", id = "" } // Parameter defaults
);
routes.MapRoute(
"DefaultWitoutCulture", // Route name
"{controller}/{action}/{id}", 开发者_StackOverflow社区 // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
I properly overlooking some thing simple but I can't get this to work for me. Or are there a better way of doing this?
To make the url without culture work I would probably create my own implementation of the route class. That could look something like this:
public class LocalizedRoute : Route
{
//Constuctors
public override RouteData GetRouteData(HttpContextBase httpContext)
{
var routeData = base.GetRouteData(httpContext);
var culture = "en";
if (httpContext.Request.Url.ToString().Contains(".nl"))
culture = "nl";
routeData.Values.Add("culture", culture);
return routeData;
}
}
Then you can use that something like this when you register your routes:
var route = new LocalizedRoute("{controller}/{action}/{id}", new MvcRouteHandler()) {
Defaults = new RouteValueDictionary(new { controller = "Home", action = "Index", id = "" }),
Constraints = new RouteValueDictionary(),
DataTokens = new RouteValueDictionary()
};
routes.Add("DefaultWitoutCulture", route);
This code could, of course, be cleaned up a bit. But this is a basic example of how I would solve your problem.
Thanks Mattias.
After some trial and errors I came op with this solution, that localize to English and Dutch, but also must important works with the old URLs - due to the constraint.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{culture}/{controller}/{action}/{id}", // URL with parameters
new { culture = "nl", controller = "Home", action = "Index", id = "" },
new { culture = @"\Aen|nl\z" } // Parameter defaults
);
routes.MapRoute(
"DefaultWitoutCulture", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { culture = "nl", controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
精彩评论