如何在VB.NET Web.Form中切换网站语言?

时间:2014-01-17 16:00:36

标签: c# asp.net vb.net routes request.querystring

我有一个简单的网站,有两种语言,英语和法语。在我的母版页中,我有一个切换到其他语言的超链接。当用户单击该超链接时,我想生成与Microsoft.com完全相同的URL!那意思是:

  1. http://www.microsoft.com/ FR-CA /locale.aspx
  2. http://www.microsoft.com/ zh-CN /locale.aspx
  3. 要做到这一点,我有点困惑!我注册了一些路由来做到这一点,但我不知道在哪里动态切换querystring变量来实现它。

    这里我在Global.asax中的Application_Start注册了RegisterRoutes:

    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
    

    我知道这是一个基本问题,但我没有找到任何答案。那么完成这项工作的好方法是什么?

    再次感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

以下代码将从用户请求/浏览器标题(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]

希望这能解决问题。 感谢