Wednesday, April 15, 2009

Response.Redirect Vs. Server.Transfer

Difference between Response.Redirect and Server.Transfer:
Response.RedirectServer.Transfer
This is more user-friendly, as the site visitor can bookmark the page that they are redirected to.Transferred pages appear to the client as a different URL than they really are. This means that things like relative links / image paths may not work if you transfer to a page from a different directory.
In Response.Redirect the browser's history is updatedIn case of Server.Transfer the browser's history is not updated
In Response.Redirect the URL is changedIn casjavascript:void(0)e of Server.Transfer the URL is not changed. Server.Transfer maintains the original URL in the browser.
Here we will always have two round-trips.The benefit of this approach is one less round trip to the server from the client browser. [1]
Has an optional second parameter, which if set to "true" stops processing the current page.Server.Transfer has an optional parameter "preserveForm" to pass the form data to the new page. If you set this to True, using a statement such as Server.Transfer ("WebForm2.aspx", True), the existing query string and any form variables will still be available to the page you are transferring to. So any posted form variables and query string parameters are available to the second page as well (however these can be cleared by passing in false for the second parameter). [2]
The server basically sends an HTTP header back to the client browser with an HTTP status code stating that the object has moved along with the new location to find it.

E.g. of HTTP Header,
HTTP/1.1 302 Object moved
Server: Microsoft-IIS/5.0
Location: somewhere/newlocation.aspx

This tells the browser that the requested resource (page) can be found at a new location, namely somewhere/newlocation.aspx. The browser then initiates another request (assuming it supports redirects) to somewhere/newlocation.aspx loading its contents in the browser. This results in two requests by the browser.
Transferring to another page using Server.Transfer conserves server resources. Instead of telling the browser to redirect, it simply changes the "focus" on the Web server and transfers the request. This means you don't get quite as many HTTP requests coming through, which therefore eases the pressure on your Web server and makes your applications run faster.
The "Response” object is property of the System.Web.UI.Page class. The type for the objects is System.Web.HttpRequest The "Server" object is property of the System.Web.UI.Page class. The type for the objects is System.Web.HttpServerUtility
Response.Redirect only support “get” method to post variables form one page to another means we need to give in query string if we want to pass some variables to next page.In Server.Transfer if we selected the second attribute to TRUE then it will post the entire variable using the post method.
This can be used to send the user to an external site. This can work on only those sites running on the server; you can't use Server.Transfer to send the user to an external site.
Resonse.Redirect can be used both for aspx and html pages.But Server.Transfer is only used for aspx pages it will not work for html pages.


[1]: One thing to be aware of is that if the first page wrote something to the Response buffer and you don’t clear it, then any output from the second page will be appended to the output of the first. This is often the cause of a weird behavior where it seems that a page is returning two different pages.

[2]: This technique is great for wizard-style input forms split over multiple pages. But there's another thing you'll want to watch out for when using the preserveForm parameter. ASP.NET has a bug whereby, in certain situations, an error will occur when attempting to transfer the form and query string values. You'll find this documented at

http://support.microsoft.com/default.aspx?id=kb;en-us;Q316920.

The unofficial solution is to set the enableViewStateMac property to True on the page you'll be transferring to, then set it back to False. This records that you want a definitive false value for this property and resolves the bug.