What's the difference between Server.transfer() and Response.Redirect() methods
Printable View
What's the difference between Server.transfer() and Response.Redirect() methods
[B]Response.Redirect[/B] involves a roundtrip back to the client.
Here's what it is:
1) You type-in the url in the browser, [url]http://www.somedomain.com/Page1.aspx[/url]. Your browser would translate it to a HTTP GET request for Page1.aspx on somedomain.com
2) The request reaches the somedomain server (through TCP/IP somehow) and the server starts processing Page1.aspx.
While processing Page1.aspx it comes across Response.Redirect("Page2.aspx"). This redirection then gets translated into a HTTP Response "302 - Object Moved" which is sent back to the client.
3) The client/browser receives the response as HTTP 302 and knows that it has to submit another HTTP GET request for Page2.aspx. The address bar of the browser changes to [url]http://www.somedomain.com/Page2.aspx[/url] and a new HTTP GET request is sent to the server for Page2.aspx on somedomain.com
[HTML]
Internet Explorer > HTTP GET Page1 > Page 1 > Response.Redirect("Page2") > Internet Explorer > request Page2 > Page 2 > Internet Explorer
[/HTML]
The 2nd request for Page2.aspx is like a new request initiated from the browser. Since, the url changes on the browser the user gets to know that there has been a change on the page it initially requested.
[B]Server.Transfer[/B] does not involve a roundtrip to the server. The transfer of control from Page1.aspx to Page2.aspx happens on the server itself and the end user doesn't come to know about it.
Using the same example,
1) You type-in the url in the browser, [url]http://www.somedomain.com/Page1.aspx[/url]. Your browser would translate it to a HTTP GET request for Page1.aspx on somedomain.com
2) The request reaches the somedomain server (through TCP/IP somehow) and the server starts processing Page1.aspx.
While processing Page1.aspx it comes across Server.Transfer("Page2.aspx"). The server would process Page2.aspx now and renders whatever comes out of it back to the client/browser.
3) The brower sees the rendered html, which actually is from Page2.aspx but the user is unknown about the change. The browser would still be showing [url]http://www.somedomain.com/Page1.aspx[/url] on the address bar.
[HTML]
Internet Explorer > HTTP GET Page1 > Page 1 > Server.Transfer("Page2") > Page 2 > Internet Explorer
[/HTML]
With Server.Transfer, the whole HTTP context is still available on Page2.aspx coz the transfer of control is within the server itself and no new request is initiated for it from the browser.
HTH!!!