Difference between Response.Redirect() and Server.Transfer() in ASP.Net
Response.Redirect() and Server.Transfer() both bring the contents from new location (specified as parameter) in web server to the client (browser). However, there are some key differences between these two commands. The main difference is that, Redirect() forces the browser to redirect to new location whereas Transfer() makes server to transfer to new location. The point here is, who does the work, it is either client or server. I am going to deal the difference in terms of the HTTP Request generated and HTTP Response received.
Steps involved in Response.Redirect()
1. Client (browser) sends a POST or GET request to the web server with details of new web location to be redirected.
2. Server checks the new location and if the location is found, it will respond back with 302 Found message. The response message format is like shown below. The message is catched by packet sniffing software called fiddler2.
HTTP/1.1 302 Found
Server: ASP.NET Development Server/9.0.0.0
Date: Fri, 08 May 2009 13:49:28 GMT
X-AspNet-Version: 2.0.50727
Location: /StateDemoApp/StatusPage.aspx
Set-Cookie: tempCookie=6; path=/
Set-Cookie: permCookie=0; expires=Fri, 08-May-2009 13:51:28 GMT; path=/
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Length: 150
Connection: Close
<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href=”%2fStateDemoApp%2fStatusPage.aspx”>here</a>.</h2>
</body></html>
3. The client knows that this message is not for display and it is ok to redirect to new location.
4. Then, client generates a new GET HTTP Request, requesting contents from new location.
5. The server then serves it as a brand new request. It does not remember the client as it is follow up of previous Response.Redirect(). In fact, no need to remember.
So, in Response.Redirect(), two round trips to the server are involved. In Response.Redirect(), we cannot pass on the form variable. Also, we cannot preserve those variables for reuse. In this method, the control is completely transferred to new page. However, the execution of current page can still be continued.
In Server.Tranfer() method, the transfer is done by server. In involves only one round trip to server. The server finds out the new location and if it ok, it serves the content. The new content from new location is served as the part of the originating application, i.e. the URL address remains same and not changed to new address unlike in Redirect() method. So, this method has got one major disadvantage that, if somebody wants to bookmark the new address, he or she will not be able to do so, as only one address is visible in address bar.