“Server.Transfer”vs “response.Redirect”
So when to use “Server.Transfer” and when to use “Response.Redirect” ?
What is importance of “preserveForm” flag in “Server.Transfer”?
Response.Redirect(URL,true) vsResponse.Redirect(URL,false) ?
Introduction
“response.redirect” and “server.transfer” helps to transfer user from one page to other page while the page is executing. But the way they do this transfer / redirect is very different.
In this short blog we will discuss about how they differ and in which scenarios we should use them.
In case you are visual guy and would like see demonstration rather than theory I would suggest to see the below facebook video which explains the difference in a more demonstrative way.
“Server.Transfer”vs “response.Redirect”
The main difference between them is who does the transfer. In “response.redirect” the transfer is done by the browser while in “server.transfer” it’s done by the server. Let us try to understand this statement in a more detail manner.In “Server.Transfer” following is the sequence of how transfer happens:-
- User sends a request to an ASP.NET page. In the below figure the request is sent to “WebForm1” and we would like to navigate to “Webform2”.
- Server starts executing “Webform1” and the life cycle of the page starts. But before the complete life cycle of the page is completed “Server.transfer” happens to “WebForm2”.
- “Webform2” page object is created, full page life cycle is executed and output HTML response is then sent to the browser.
While in “Response.Redirect” following is the sequence of events for navigation:-
- Client (browser) sends a request to a page. In the below figure the request is sent to “WebForm1” and we would like to navigate to “Webform2”.
- Life cycle of “Webform1” starts executing. But in between of the life cycle “Response.Redirect” happens.
- Now rather than server doing a redirect , he sends a HTTP 302 command to the browser. This command tells the browser that he has to initiate a GET request to “Webform2.aspx” page.
- Browser interprets the 302 command and sends a GET request for “Webform2.aspx”.
In other words “Server.Transfer” is executed by the server while “Response.Redirect” is executed by thr browser. “Response.Redirect” needs to two requests to do a redirect of the page.
So when to use “Server.Transfer” and when to use “Response.Redirect” ?
Use “Server.Transfer” when you want to navigate pages which reside on the same server, use “Response.Redirect” when you want to navigate between pages which resides on different server and domain.
Below goes the consolidated table with all the differences as discussed at the top.
What is importance of “preserveForm” flag in “Server.Transfer”?
“Server.Transfer” helps to redirect from one page to other page. If you wish to pass query string and form data of the first page to the target page during this redirection you need to set “preserveForm” to “true” as shown in the below code.
Server.Transfer("Webform2.aspx",true);
By default the value of “preserveForm” is “true”.
Response.Redirect(URL,true) vsResponse.Redirect(URL,false) ?
Response.Redirect(URL,true) :- Client is redirected to a new page but the processing of the current page is aborted.
Below is a facebook video whichdemonstrates practically the difference between server.transfervsresponse.redirect .A big thanks to www.questpond.com to allow me to publish this videos for free on facebook.
No comments:
Post a Comment