请帮我在我的语言的URL中创建查询字符串!包括完整的代码

时间:2012-01-30 04:15:21

标签: asp.net vb.net visual-studio optimization

我设法使用Visual Studio 2010 / VB / ASP.NET 4.0以9种语言创建了我公司的网站。我相信我正在使用会话。但您可以确定是否看到下面提供的代码。我知道这个消息很长,但我真的需要帮助。

这是一个多语种网站,我设法在主页上放置了标记。单击标记时,页面的文本将更改为该语言。单击法国标志时,它会从我的apps_GlobalResources文件夹中的“FR”.resx资源文件中获取信息。对于最终用户的整个会话,它保留在该语言上。大!好吧,不是那么好。

例如, about.aspx的网址仍为about.aspx 。当然,文本更改为法语,但我被告知,如果客户选择法语,建议使其看起来像, domain.com/about.aspx?lang=FR

但我不知道该怎么做。如果有人能够指引我朝着正确的方向前进,那么真诚地赞赏!!!

作为参考,如果有人能够深入研究这个问题,这可能是一个简单的问题,我在这里使用的所有文件都包含在下面 - 而且它们有点冗长< / em>的)

---- Homepage.master(我如何调用语言 - 只需2个样本) -

  <asp:LinkButton ID="LinkButton6" runat="server"
  CommandArgument="de" OnClick="RequestLanguageChange_Click"
  class="flagbutton">      
  <asp:Image ID="Image7" runat="server" ImageUrl="~/images/flagde.png"
  tooltip="View this website in Deutsch" title="View this website in Deutsch"/>
  <img class="map" src="images/flaghoverde.png" alt=""/>
  </asp:LinkButton>

  <asp:LinkButton ID="LinkButton5" runat="server"
  CommandArgument="fr" OnClick="RequestLanguageChange_Click"
  class="flagbutton">      
  <asp:Image ID="Image6" runat="server" ImageUrl="~/images/flagfr.png"
  tooltip="Voir ce site en français" title="Voir ce site en français"/>
  <img class="map" src="images/flaghoverfr.png" alt=""/>
  </asp:LinkButton>

--------------- homepage.master.vb背后的代码---------------------

     Partial Public Class Homepage
    Inherits System.Web.UI.MasterPage
    Protected Sub Page_Load(sender As Object, e As EventArgs)
    End Sub

     Protected Sub RequestLanguageChange_Click(sender As Object, e As EventArgs)
    Dim senderLink As LinkButton = TryCast(sender, LinkButton)

    'store requested language as new culture in the session
    Session(Udev.MasterPageWithLocalization.Classes.Global.SESSION_KEY_CULTURE) = 
    senderLink.CommandArgument

    'reload last requested page with new culture
    Server.Transfer(Request.Path)
   End Sub
   End Class

--------------------- App_Code文件夹中的BasePage.vb --------------------

  Imports Microsoft.VisualBasic
  Imports System
  Imports System.Data
  Imports System.Configuration
  Imports System.Globalization
  Imports System.Threading
  Imports System.Web
  Imports System.Web.Security
  Imports System.Web.UI
  Imports System.Web.UI.WebControls
  Imports System.Web.UI.WebControls.WebParts
  Imports System.Web.UI.HtmlControls

  Namespace Udev.MasterPageWithLocalization.Classes
''' <summary>
''' Custom base page used for all web forms.
''' </summary>
Public Class BasePage
    Inherits Page
    Protected Overrides Sub InitializeCulture()
        'retrieve culture information from session
        Dim culture__1 As String =  
             Convert.ToString(Session([Global].SESSION_KEY_CULTURE))

        'check whether a culture is stored in the session
        If culture__1.Length > 0 Then
            Culture = culture__1
        End If

        'set culture to current thread
        Thread.CurrentThread.CurrentCulture =  
        CultureInfo.CreateSpecificCulture(culture__1)
        Thread.CurrentThread.CurrentUICulture = New CultureInfo(culture__1)

        'call base class
        MyBase.InitializeCulture()
    End Sub
   End Class
  End Namespace

---------------------- App_Code文件夹中的Culture.vb ------------------- -

  Imports Microsoft.VisualBasic
  Imports System
  Imports System.Data
  Imports System.Configuration
  Imports System.Web
  Imports System.Web.Security
  Imports System.Web.UI
  Imports System.Web.UI.WebControls
  Imports System.Web.UI.WebControls.WebParts
  Imports System.Web.UI.HtmlControls

  Namespace Udev.MasterPageWithLocalization.Classes
''' <summary>
''' This class provides ISO definitions for all cultures that are supported by this      
 application.
''' </summary>
Public Structure Culture
    'German - Switzerland definition
    Public Const DE As String = "de"
    'English - Great Britain definition
    Public Const EN As String = "en"
     End Structure
  End Namespace

------------------ App_Code文件夹中的Global.vb ----------------------- -

  Imports System
  Imports System.Data
  Imports System.Configuration
  Imports System.Web
  Imports System.Web.Security
  Imports System.Web.UI
  Imports System.Web.UI.WebControls
  Imports System.Web.UI.WebControls.WebParts
  Imports System.Web.UI.HtmlControls

  Namespace Udev.MasterPageWithLocalization.Classes
''' <summary>
''' Summary description for Global
''' </summary>
Public Structure [Global]
    Public Const SESSION_KEY_CULTURE As String = "culture"
    End Structure
  End Namespace

1 个答案:

答案 0 :(得分:1)

在OnInit的顶部添加这样的内容(最不喜欢,因为它意味着代码复制)。理想情况下,此代码将转到您的基页的oninit(首选),或者您的主页的oninit(不太喜欢)。无论哪种方式,它应该按照你想要的方式工作。

if(this.Request.Querystring["lang"] == null)
{
    string path = this.Request.Url.AbsolutePath;
    string query = this.Request.Url.Query;
    if(query.length == 0)
        query = "?lang="; // concatenate language code here
    else
        query += "&lang="; // conc. lang. code here
    this.Response.Redirect(path + query, true);
    return;
}

这意味着:如果lang不在查询中,请刷新页面,但这次是在查询中使用lang。

我希望我没有误解这个问题。


编辑:VB版本(哦,这很痛苦;我实际上必须为此创建一个VB项目):

Protected Overrides Sub OnInit(e As System.EventArgs)
    MyBase.OnInit(e)

    If Me.Request.QueryString("lang").Length = 0 Then
        Dim path As String = Me.Request.Url.AbsolutePath
        Dim query As String = Me.Request.Url.Query

        If query.Length = 0 Then
            query = "?lang=" ' + add lang here
        Else
            query += "&lang=" ' add lang
        End If

        Me.Response.Redirect(path + query, True)
        Return
    End If

End Sub

所以,看起来您的基类中没有OnOnit,但您可以覆盖它。