Friday, December 31, 2010

Difference Between Server.Transfer and Response.Redirect

Server.Transfer and Response.Redirect are used to transfer the control from one page to other. But there are some differences in both which are listed below.

Response.Redirect(..) :- Response.Redirect sends HTTP code 302 down to the users browser along with the new URL location of the wanted page.HTTP Code 302 actually means ' The requested resource resides temporarily under a different URI'.
After browser receives this code it send a request to server for that resource or page.
This actually causes two requests to the server, first one to the original URL, and second to the new URL that is suggested via 302 response.

Server.Transfer(..) :- In contrast to all this when we call Server.Transfer we do not initiate another request to the server, but the original request is simply rewritten and transferred to some other page on the same server.
(This off course means that we can use it only to transfer requests to the pages on the same server, not to some other servers and we can only transfer to .aspx pages and not other page types like HTML, php etc).

When Repsponse.Redirect("Temp.aspx") line execute server say to client browser to send a request for Temp.aspx. Than Client browser request for Temp.aspx.While in case of Server.Transfer("Temp.aspx") Server does not say anything to client browser just send Temp.aspx page.

1) In Response.Redirect previous page is not accessible while in Server.Transfer it is optional.
Server.Transfer(URL,bPreserveForm);

2) Server.Transfer use only within the server.But Response.Redirect can be use ouside the server.But it should be a full path like http://www.google.com only www.google.com will not work

3)Resonse.Redirect can be used both for aspx and html or php pages.But Server.Transfer is only used for .aspx pages it will not work for html pages.

4) Response.Redirect use only 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. But in case of Response.Transfer you can use post method with the help of Context.Item[“Message”].

5)In Response.Redirect you see the full url with values in browser and in Server.Transfer you can not see it.

6)Secondly, Server.Transfer maintains the original URL in the browser. This can really help streamline data entry techniques, although it may make for confusion when debugging.

8)In case of Server.Transfer you can use Context.Items["Message"] to store and get the values but in case of Response.Redirect you can not save values with this method.

9)One thing to be careful about when using the Server.Transfer is to clear the HttpResponse object with Response.Clear method on the transferred page to avoid any output from the first page to be shown on the second page.

10)In Server.Transfer url remain same there Bookmark can not be used while in case of Response .Redirect Bookmark can be use.

Server Transfer

1. Client Request Page HelloWorld.ASPX
2. Server.Transfer -> Server send a different page to the client
3. Client Receives Page still thinking it's HelloWorld.ASPX.
4. Client's URL (Address bar) remains HelloWorld.ASPX since the page was
sent on the server side and the client doesn't know a different page was
sent.

Response.Redirect

1. Client Requests Page HelloWorld.ASPX
2. Response.Redirect -> Server sends a HTTP header informing that the
user should redirect.
3. Client sees the redirect notice and requests
AnotherPage.ASPX
4. Server sends AnotherPage.ASPX
5. Client's URL (address bar) shows AnotherPage.ASPX

Don't confuse Server.Transfer with Server.Execute, which executes the page and returns the results. It was useful in the past, but, with ASP.NET, it's been replaced with fresher methods of development. Ignore it.

No comments:

Followers

Link