[ASP.NET]Response.Redirect + Request.UrlReferrer

[ASP.NET]Response.Redirect + Request.UrlReferrer

我們有個專案,需要透過Request.UrlReferrer取得上一個頁面Host的資料,說明如下,

Default.aspx => Default2.aspx(要取得Request.UrlReferrer)

於是在Default.aspx的Page_Load事件中,去Redirect到Default2.aspx,可是在Default2.aspx中取得到Request.UrlReferrer居然是Nothing。

所以讓Default.aspx的PostBack時,再Redirect到Default2.aspx,就可以取到Request.UrlReferrer了。

Default.aspx

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    </div>
    </form>
</body>
</html>


Default.aspx.vb

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Page.IsPostBack Then
            'postback時,才轉過去default2.aspx
            Response.Redirect("default2.aspx")
        Else
            'Get時,先到Client然後再Postback
            Page.ClientScript.RegisterStartupScript(Me.GetType(), "redir", "form1.submit();", True)
        End If
    End Sub
End Class


Default2.aspx.vb

Partial Class Default2
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Try
            Response.Write("host:" & Request.UrlReferrer.Host.ToLower().ToString().Trim())
        Catch ex As Exception
            Response.Write(Err.ToString)
        End Try

    End Sub
End Class

Hi, 

亂馬客Blog已移到了 「亂馬客​ : Re:從零開始的軟體開發生活

請大家繼續支持 ^_^