Response.Redirect vs Server.Transfer Difference

Response.Redirect And Server.Transfer Difference





FOR REDIRECT



we want to redirect the request to some plain HTML pages on our server or to some other web server

we DON'T care about causing additional ROUNDTRIPS to the server on each request

we do not need to preserve Query String and Form Variables from the original request

we want our users to be able to see the NEW redirected URL where he is redirected in his browser


FOR SERVER.TRANSFER





we want to transfer current page request to another .aspx page on the same server

we want to preserve server resources and AVOID the unnecessary ROUNDTRIPS to the server

we want to preserve Query String and Form Variables (optionally)

we DON'T need to show the real URL where we redirected the request in the users Web Browser






.using Server.Tansfer Pass Value



Ori Page :


<body>
    <form id="form1" runat="server">
        <div>
            Name: <asp:TextBox runat="server" ID="txtName" ></asp:TextBox>
        </div>
        <br />
    <div>
        
    Redirect : <asp:Button runat="server" ID="btnRedirect" Text="Redirect" OnClick="btnRedirect_Click"/>
    </div>
    <br/>
    <div>    
        Transfer : <asp:Button runat="server" ID="btnTransfer" Text="Transfer" OnClick="btnTransfer_Click"/>
        <asp:CheckBox runat="server" ID="chkPreserveForm" Text="Preserve Form ?"/>
    </div>


    </form>
</body>



 protected void btnRedirect_Click(object sender, EventArgs e)
        {
            Response.Redirect("Destination.aspx");
        }


        protected void btnTransfer_Click(object sender, EventArgs e)
        {
            Server.Transfer("Destination.aspx", chkPreserveForm.Checked);
        }

Page Load :
 if(PreviousPage == null)return;
            var txtName = PreviousPage.FindControl("txtName") as TextBox;
            if (null != txtName)
            {
                lblName.Text = txtName.Text;
            }





Destination Page:


 <div>
    Welcome ! <asp:Label runat="server" ID="lblName"></asp:Label><br/>
        Your ID: <asp:Label runat="server" ID="lblId"></asp:Label> <br />
<asp:Button runat="server" Text="Go Back" ID="btnGoBack" OnClick="btnGoBack_Click"/>
    </div>



 protected void Page_Load(object sender, EventArgs e)
{
 if(PreviousPage == null)return;
            var txtName = PreviousPage.FindControl("txtName") as TextBox;
            if (null != txtName)
            {
                lblName.Text = txtName.Text;
            }
}


Conclusion :

  1. .Redirect will lost querystring and form data
  2. .using server.Transfer can preserve querystring optionally
  3. .redirect will see the url changed (as there is a roundtrip go back to client ), server transfer only happens in server.
  4. .the PREVIOUSPAGE property only available before loaded the destination page ,once page loaded, PREVIOUSPAGE property will be cleaned .for example , i want to click "go back" button go back to previous page ,which is impossible.

你可能感兴趣的:(response)