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

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

问题描述

这是一个关于 asp.net mvc 多语言网址/路由和 SEO 最佳实践/好处的两部分问题......

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.

就 SEO 而言,这两种方式之间的推荐方法是什么?

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 部分)

为了实现 Fashion 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 显示在适当的语言,而不必在 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 中分配 lang.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天全站免登陆