How Do I Post and then redirect to an external URL from ASP.Net?

前端 未结 6 901
说谎
说谎 2021-02-05 01:23

ASP.NET server-side controls postback to their own page. This makes cases where you want to redirect a user to an external page, but need to post to that page for some reason (

6条回答
  •  一个人的身影
    2021-02-05 01:53

    I needed to open in the same window, dealing with possible frame issues from the original page, then redirecting to an external site in code behind:

    Private Sub ExternalRedirector(ByVal externalUrl As String)
        Dim clientRedirectName As String = "ClientExternalRedirect"
        Dim externalRedirectJS As New StringBuilder()
    
        If Not String.IsNullOrEmpty(externalUrl) Then
            If Not Page.ClientScript.IsStartupScriptRegistered(clientRedirectName) Then
                externalRedirectJS.Append("function CheckWindow() {")
                externalRedirectJS.Append("   if (window.top != window) {")
                externalRedirectJS.Append("       window.top.location = '")
                externalRedirectJS.Append(externalUrl)
                externalRedirectJS.Append("';")
                externalRedirectJS.Append("       return false;")
                externalRedirectJS.Append("   }")
                externalRedirectJS.Append("   else {")
                externalRedirectJS.Append("   window.location = '")
                externalRedirectJS.Append(externalUrl)
                externalRedirectJS.Append("';")
                externalRedirectJS.Append("   }")
                externalRedirectJS.Append("}")
                externalRedirectJS.Append("CheckWindow();")
    
                Page.ClientScript.RegisterStartupScript(Page.GetType(), clientRedirectName, externalRedirectJS.ToString(), True)
            End If
        End If
    End Sub
    

提交回复
热议问题