加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – asp.net mvc多语言urls /路由

发布时间:2020-12-16 00:26:36 所属栏目:asp.Net 来源:网络整理
导读:这是关于asp.net mvc多语言url /路由和SEO最佳做法/好处的两个部分问题… 问题第1部分) 我被要求创建一个新的ASP.NET MVC网站,将支持最少(最初)两种语言(英语和法语),或许在将来,3种语言… 就本地化应用程序(标签,jQuery错误等),事情应该使用资源文件,
这是关于asp.net mvc多语言url /路由和SEO最佳做法/好处的两个部分问题…

问题第1部分)

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

就本地化应用程序(标签,jQuery错误等),事情应该使用资源文件,我已经发现了很多例子,但我的关注/问题是更多关于URL。

在SEO方面,这两种时尚之间推荐的方法是什么?

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

使用其中一个是否有一个已知的问题/惩罚?

还是那么小,变得无关紧要!

问题第2部分)

为了实现时尚2,我已经在这里找到了一篇文章:
ASP.NET MVC – Localization route

但我会好奇地找到如何实现时尚1。

有谁有任何链接?

另外,据我所知,URL Rewriting不是我正在寻找,因为我不想“重定向”用户…我只是希望网址显示在适当的语言,而不必显示文化在网址

提前感谢您的任何帮助!

解决方法

您可以创建具有本地化逻辑的基本控制器,如下所示:
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;

     }

 }

如果你想忽略文化文件夹,不要在RouteDate中分配lang,上述控制器满足你的问题的时尚2。要实现时尚2,你必须添加路由文化如下:

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})?"}
            );

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读