我有一个简单的网站,有两种语言,英语和法语。在我的母版页中,我有一个切换到其他语言的超链接。当用户单击该超链接时,我想像 Microsoft.com 一样生成 URL!这意味着:
- http://www.microsoft.com/fr-ca/locale.aspx
- http://www.microsoft.com/en-us/locale.aspx
要做到这一点,我有点困惑!我注册了一些路由来做到这一点,但我不知道我在哪里动态切换查询字符串变量来实现这一点。
这是我的RegisterRoutes from Application_Start in Global.asax:
Sub RegisterRoutes(ByVal routes As RouteCollection)
Dim language As String = "en-us"
routes.MapPageRoute("Home", "home/{language}/{ReturnUrl}", "~/Default.aspx", True,
New RouteValueDictionary(New With {.language = language, .ReturnUrl = "default.aspx"}))
routes.MapPageRoute("DetailsUser", "info-user/{language}/{id}", "~/DetailUsager.aspx", True,
New RouteValueDictionary(New With {.language = language, .id = ""}))
routes.MapPageRoute("Contact", "contact-us/{language}", "~/ContactForm.aspx", True,
New RouteValueDictionary(New With {.language = language}))
routes.MapPageRoute("SwitchLanguage", "{language}", "~/Home.aspx", True,
New RouteValueDictionary(New With {.language = language}))
End Sub
我知道这是一个基本问题,但我找不到任何答案。那么做到这一点的好方法是什么?
再次感谢您的帮助!
以下代码将从用户请求/浏览器标头 (en-US) 获取语言
VB.Net
For Each s As String In Me.Request.UserLanguages
Response.Write(s)
Next
C#
foreach (string s in this.Request.UserLanguages) {
Response.Write(s);
}
当前语言是数组中的第一个:
VB.Net
Me.Request.UserLanguages(0)
C#
this.Request.UserLanguages[0]
希望这能回答它。谢谢