如何在 ASP MVC 网站中使用路由来本地化为两种语言 - 但保留现有 URL
我们有几个 ASP MVC 网站,仅使用标准 VS 模板默认设置 - 按需要工作。但现在我想本地化这些网站(它们现在是荷兰语,我将添加英语) 我想使用路由而不是资源,因为: 1. 不同语言在内容、页数等方面会有所不同。 2.内容以文字为主。
我希望 URL 看起来像这样 - www.domain.com/en/Home/Index、www.domain.nl/nl/Home/Index。 但最后一个也应该与 - www.domain.nl/Home/Index - Witch 一起使用,这是令人兴奋的 URL。
我已经从这篇博文中实现了 Phil Haacks 区域 ViewEngine - http:// /haacked.com/archive/2008/11/04/areas-in-aspnetmvc.aspx。但只将英文网站放在这些区域中,并保留荷兰语的旧结构。女巫是菲尔斯的默认后备。 但问题在于我必须为两种语言复制控制器。
所以我尝试了此步骤中描述的工作方法 - ASP.NET MVC - 本地化路线 。它与 ?en? 一起工作正常吗?和 /nl/ 但不使用旧 URL。
在 global.asax 中使用此代码时,没有区域性的 URL 不起作用。
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}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
我正确地忽略了一些简单的事情,但我无法让它为我工作。或者有更好的方法吗?
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}", // 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了使 url 不受文化影响,我可能会创建自己的路由类实现。这可能看起来像这样:
然后你可以在注册路由时使用类似这样的东西:
当然,这个代码可以稍微清理一下。但这是我如何解决您的问题的一个基本示例。
To make the url without culture work I would probably create my own implementation of the route class. That could look something like this:
Then you can use that something like this when you register your routes:
This code could, of course, be cleaned up a bit. But this is a basic example of how I would solve your problem.
谢谢马蒂亚斯。
经过一番尝试和错误后,我使用了这个解决方案,该解决方案本地化为英语和荷兰语,但由于限制,还必须与旧 URL 一起使用。
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.