asp.net mvc的多语言网址/路由 [英] asp.net mvc multilanguage urls/routing

查看:89
本文介绍了asp.net mvc的多语言网址/路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是关于asp.net的MVC多语言网址/路由和搜索引擎优化的最佳实践/待遇。

This is a two part question regarding asp.net mvc multilanguage urls/routing and SEO best practices/benefits…

问第1部分)

我被要求创建一个新的ASP.NET MVC网站,将支持最低(起初)两种语言(英语和法语)也许在未来,3种语言...

I’m being asked to create a new ASP.NET MVC website that will support a minimum (at first) of two languages (English and French) perhaps in the future, 3 languages…

至于本地化应用(标签,jQuery的错误等),事情应该使用资源文件是好的,我已经发现许多这样的例子......但我关心的/的问题是更多的URL。

As far as localizing the application (labels, jQuery errors, etc) things should be ok using resource files and I’ve found many examples on this…but my concern/question is more about the URLs.

在搜索引擎优化方面,什么是这两个时尚的建议的方法?

In terms of SEO, what is the recommended approach between these two fashions?

Fashion 1 (no culture folder)  
www.mydomain.com/create-account 
www.mydomain.com/creer-un-compte

Fashion 2 (with built in culture folder)
www.mydomain.com/create-account 
www.mydomain.com/fr/creer-un-compte <--notice the "fr" folder 

有没有使用一个比其他已知问题/惩罚?

Is there a known issue/penalty in using one over the other?

抑或是如此之小,它变得无关紧要!

Or is it so small that it becomes irrelevant!

问第2部分)

要实现时尚2,我已经找到了一个文章在这里:
ASP.NET MVC - 本地化路线

To achieve Fashion 2, I’ve already found an article here: ASP.NET MVC - Localization route

不过,我很好奇找到如何实现时尚1。

But I’d be curious to find how to achieve Fashion 1.

有没有人有任何联系?

此外而且据我所知,URL重写不可以我在寻找什么的,因为我不希望重定向用户......我只是希望在URL中显示在适当的语言,而不必展现文化的网址

In addition and as far as I know, URL Rewriting is not what I’m looking for since I do not wish to "redirect" users…I simply want the urls to be showing in the appropriate language without having to show the culture in the url

在此先感谢有这方面的帮助!

Thanks in advance for any help on this!

推荐答案

您可以创建具有如下定位逻辑基础的控制器:

you can create base controller that have the localization logic as below:

 public abstract class LocalizedController : Controller
 {
     protected override void ExecuteCore()
     {
         HttpCookie cookie;
         string lang = GetCurrentCulture();
         Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang, false);

         // set the lang value into route data
         RouteData.Values["lang"] = lang;

         // save the location into cookie
         cookie = new HttpCookie("DPClick.CurrentUICulture",
             Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName)
             {
                 Expires = DateTime.Now.AddYears(1)
             };

         HttpContext.Response.SetCookie(cookie);  
         base.ExecuteCore();
     }

     private  string  GetCurrentCulture()
     {
         string lang;

         // set the culture from the route data (url)

         if (RouteData.Values["lang"] != null &&
            !string.IsNullOrWhiteSpace(RouteData.Values["lang"].ToString()))
         {
             lang = RouteData.Values["lang"].ToString();
             if (Localization.Locales.TryGetValue(lang, out lang))
             {
                 return lang;
             }
         }
         // load the culture info from the cookie
         HttpCookie cookie = HttpContext.Request.Cookies["DPClick.CurrentUICulture"];
         if (cookie != null)
         {
             // set the culture by the cookie content
             lang = cookie.Value;
             if (Localization.Locales.TryGetValue(lang, out lang))
             {
                 return lang;
             }

         }
         // set the culture by the location if not speicified
         lang = HttpContext.Request.UserLanguages[0];
         if (Localization.Locales.TryGetValue(lang, out lang))
         {
             return lang;
         }
         //English is default
         return Localization.Locales.FirstOrDefault().Value;

     }

 }

以上控制器满足你的问题的方式2,如果你想忽略文化文件夹只是没有在RouteDate分配郎。 offcourse实现时尚2,你必须添加路由文化如下:

The above controller satisfy fashion 2 of your question if you want to ignore the culture folder just don't assign the lang in the RouteDate. offcourse to achieve fashion 2 you have to add routing for culture as below:

            routes.MapRoute(
            "Localization", // Route name
            "{lang}/{controller}/{action}/{id}", // URL with parameters
            new {controller = "Default", action = "Index", id = UrlParameter.Optional}, // Parameter defaults
            new {lang = @"\w{2,3}(-\w{4})?(-\w{2,3})?"}
            );

这篇关于asp.net mvc的多语言网址/路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆