asp.net-mvc – asp.net mvc多语言urls /路由
|
这是关于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,我已经在这里找到了一篇文章: 但我会好奇地找到如何实现时尚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})?"}
); (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-mvc – 如何在我自己的自定义助手中使用
- asp.net-mvc – 如何更改ASP.NET MVC中的默认验证
- asp.net-mvc – 什么是应用程序洞察遥测(未配置)
- ASP.NET Web站点项目中的“复制本地”等效参考
- asp.net – “共享主机中的”LOG_BACKUP“的数据
- One to One 的数据库模型设计与NHibernate配置
- Asp.NET 生成静态页面并分页的代码
- asp.net-web-api – 如何创建角色并将用户添加到
- asp.net-mvc – 验证:Model或ViewModel
- asp.net-web-api – 首先使用ASP.NET Web API的E
