Difference between Response.Redirect and Server.Transfer in ASP.Net

Hi in this post, I will be exploring the Response.Redirect and Server.Transfer redirecting options available in ASP.NET

Response.Redirect is used to redirect the page to a page in the same server or different server
Server.Transfer is used to redirect the page to a page in the same server only

Example :
RR - Response.Redirect("LoginPage.aspx")
Response.Redirect("LoginPage.htm")
Response.Redirect("yourSiteName.com/LoginPage.aspx")

ST - Server.Transfer("LoginPage.aspx")

Working

Let, in Source.aspx there is a button (or LinkButton or ImageButton or any control that causes a post back) and the button event contains the code Response.Redirect or Server.Transfer

BLOGPIC - Copy


Response.Redirect execution

1 When the user clicks on the button a request will be sent to the server for the
execution of button event code .
2 Button click event code (containing Response.Redirect)
will be executed by the server and the current page processing will
be stopped
3 Response will be rendered to the browser asking the browser
to submit the request for Destination.aspx page
4 The browser will request the Destination.aspx page
5 The server processes the request for Destination.aspx page
6 The server sends a response to the browser with the Destination.aspx page

Server.Transfer execution

1 When the user clicks on the button a request will be sent to the server for the
execution of button event code.
2 Button click event code (containing Server.Transfer)
will be executed by the server.
3 The server will processes the Destination.aspx
4 The output of the Destination.aspx will be rendered to the browser


Other differences

RR -  Form variables of the source page are not accessible in destination page
ST -  Form variables of the source page are accessible in destination page
(by using the Server.Transfer overloaded method  containing   preserveForm property )

RR - Execution is slow since there are two roundtrips and hence there load on server
ST - Execution is fast since there is only one roundtrip and hence there is less load on the server

RR - Refreshing of page does not cause any error.
ST - Refreshing of page causes error.

RR - RR changes the URL in the browser since browser is involved.
ST - ST doesnt change the URL in the browser since browser is not involved.


Uses

RR is to be used in operations like data insertion so that even  if the user refreshes the page then only confirmation will be rendered back ie executing steps 4,5 and 6.
ST  is to be used when the URL is to be hidden from the user.


Note -
Request is the message that the browser sends to the server
Response is the message that the server sends back to the browser
Round trip is a combination of request to the server and the response to the browser

Further reading -
http://pubs.logicalexpressions.com/Pub0009/LPMArticle.asp?ID=214
http://msdn.microsoft.com/en-in/library/ms524309(v=vs.90).aspx