简体   繁体   中英

VB.NET redirection loop error

I have the following code on my multilingual website: (MasterPage.master.vb)

Dim pageUrl As String = HttpContext.Current.Request.Url.AbsolutePath

Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load

    Dim strLanguage As String = Session("Language")

    If Not IsPostBack Then

        Response.Redirect(String.Format("{0}?Language={1}", pageUrl, strLanguage))

    End If

    Response.Write(String.Format("{0}?Idioma={1}", pageUrl, strLanguage))
End Sub

It will display the querystrings: '?Language=es' or '?Language=en' at the end of the current URL, but when I run the web, the browser displays the message: This page has a redirect loop (ERR_TOO_MANY_REDIRECTS).

Why is this happening? How can i fix this?

Thanks in advance

Edit: I also tried with:

If IsPostBack Then

but when i do that, the querystring doesn't appear at the end of the URL.

Edit 2 (solved): It works at last! Thank you very much zed, the final code is as follows:

     Dim pageUrl As String = HttpContext.Current.Request.Url.AbsolutePath

Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load

    Dim strLanguage As String = Session("Language")

    If Not IsPostBack Then

        If Request.QueryString("Language") Is Nothing Then
            Response.Redirect(String.Format("{0}?Language={1}", pageUrl, strLanguage))
        End If
    Else
        If Not Request.QueryString("Language") Is Nothing Then
            Response.Redirect(String.Format("{0}?Language={1}", pageUrl, strLanguage))
        End If
    End If

    Response.Write(String.Format("{0}?Language={1}", pageUrl, strLanguage))
End Sub

You are always redirecting at first page load. See:

    If Not IsPostBack Then

        Response.Redirect(String.Format("{0}?Language={1}", pageUrl, strLanguage))

    End If

When you first visit any page that uses the master page, you will be redirected again, and again, and again (by the way, a redirection is not a postback)

Depending on what you need to achieve, you may have to make some kind of check before doing that Redirect, or it will always occur. You could ask, for instance, if Language is not present in the QueryString, and only then do the redirection:

If Not IsPostBack Then
    If Request.QueryString("Language") Is Nothing Then
        Response.Redirect(String.Format("{0}?Language={1}", pageUrl, strLanguage))
    End If
End If

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM