Monday, October 22, 2007

Differences between Server.Transfer and Response.Redirect::::::::::::::::::::::

Redirect and Transfer both cause a new page to be processed. But the interaction between the client (web browser) and server (ASP.NET) is different in each situation.

Redirect: A redirect is just a suggestion – it’s like saying to the client “Hey, you might want to look at this”. All you tell the client is the new URL to look at, and if they comply, they do a second request for the new URL.
If you want to pass state from the source page to the new page, you have to pass it either on the URL (such as a database key, or message string), or you can store it in the Session object (caveat: there may be more than one browser window, and they’ll all use the same session object).
Transfer: A transfer happens without the client knowing – it’s the equivalent of a client requesting one page, but being given another. As far as the client knows, they are still visiting the original URL.


Sharing state between pages is much easier using Server.Transfer – you can put values into the Context.Items dictionary, which is similar to Session and Application, except that it lasts only for the current request. (search for HttpContext in MSDN). The page receiving postback can process data, store values in the Context, and then Transfer to a page that uses the values.

Awesome Difference::::::::
Response.Redirect sends message to browser saying that the browser should request some other page so basically it means that:
1. Browser is on page default1.aspx on which there is a Response.Redirect("") command which means that the server sends response (message) to browser that browser should request some other page default2.aspx

2. Browser sends request to server in order to get this page default2.aspx
3. Server sends page default2.aspx to browser

Server.Transfer doesn't tell the browser to request page default2.aspx. It just sends page default2.aspx, so e.g., browser address bar still shows the original page's URL.

Roundtrip is the combination of a request being sent to the server and response being sent back to browser.

Request is the message that the browser sends to the server and Response is the message the server sends back to the browser.

refernece: http://weblogs.asp.net/rosherove/archive/2003/08/11/23550.aspx

No comments: