Difference between Response.Redirect and Server.Transfer in asp.net

Introduction: In previous articles i explained the Difference between Delete and Truncate in sql server and 15 main Difference between DataSet and DataReader in asp.net and  Difference between DataSet and DataTable in asp.net  and 20 main differences between Stored procedures and Functions in Sql Server. Similarly What is the difference between ‘Response.Redirect’ and ‘Server.Transfer’  is one of the most important interview questions asked to freshers.
 I have tried to explain some of the main differences between the two so that freshers can better understand the difference between them. Both the “Server” and “Response” are objects of asp.net. ‘Server. Transfer’ and ‘Response. Redirect’ both are used for the same purpose i.e. to transfer from one page to another page but still there are some differences that are as follows:

Response.Redirect vs Server.Transfer

1.     ‘Response. Redirect’ sends message to the browser saying it to move to some different page, while ‘Server. Transfer’ does not send any message to the browser but rather it redirects the user directly from the server itself. So in case of ‘Server. Transfer’ there is no round trip while Response. Redirect has a round trip and hence puts extra load on server.

2.     Using ‘Server. Transfer’ we cannot redirect to external websites or website pages. E.g. if your website is www.webcodeexpert.com then you cannot use ‘Server. Transfer’ to move to www.google.com but yes, you can move to internal pages  www.webcodeexpert.com/asp.net, i.e. within the websites. Cross server redirection is possible only by using ‘Response.Redirect’ i.e. it allows redirection to internal as well as external websites and website pages.

3.     With ‘Response. Redirect’ we can redirect the user to the both type of pages .html or .aspx e.g. Response. Redirect (“OtherPage.html”) OR Response. Redirect (“OtherPage.aspx”) But in case of ‘Server. Transfer’ we can redirect user to .asp or .aspx pages only e.g. Server. Transfer (“OtherPage.asp”) OR Server. Transfer (“OtherPage.aspx”) not to Server. Transfer (“OtherPage.html”).

4.     In ‘Server. Transfer’ URL doesn’t change but in case of ‘Response. Redirect’ URL changes.

5.     When we want to allow our website’s URL can be copied then ‘Response. Redirect’ is better but for security reasons ‘Server. Transfer’ is better because URL cannot be copied.

6.     ‘Response. Redirect’ has a round trip but ‘Server.Transfer’ has no round trip. (Roundtrip is the combination of a request being sent to the server and response being sent back to browser.)

7.     ‘Server. Transfer’ is a server process whereas ‘Response. Redirect’ is a client process.

8.     ‘Server.Transfer’ preserves Query String and Form Variables (optionally). ‘Response. Redirect’ doesn’t preserve Query String and Form Variables from the original request.

9.     ‘Server. Transfer’ is faster since there is one less round trip as compared to ‘Response. Redirect’. 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.

10. ‘Server. Transfer’ allow us to directly access the values, controls and properties of the previous page which we can’t do with ‘Response. Redirect’. The ’Server. Transfer’ method also has a second parameter—"preserveForm". If you set this to True, using a statement such as Server. Transfer ("OtherPage.aspx", True), the existing query string and any form variables will still be available to the page you are transferring to. For example, if your CurrentPage.aspx has a TextBox control called TextBox1 and you transferred to OtherPage.aspx with the preserveForm parameter set to True, you'd be able to retrieve the value of the original page’s TextBox control by referencing Request. Form ("TextBox1").

11. ‘Response. Redirect’ involves a roundtrip to the server whereas ‘Server. Transfer’ conserves server resources by avoiding the roundtrip. It just changes the focus of the web server to a different page and transfers the page processing to a different page. Roundtrip means in case of ‘Response. Redirect’ it first sends the request for the new page to the browser then browser sends the request for the new page to the web server only then a your page changes But in case of ‘Server. Transfer’ it directly communicate with the server to change the page hence it saves a roundtrip in the whole process.

Now over to you:

"If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linked in and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned for more technical updates."
Previous
Next Post »

14 comments

Click here for comments
manasa
admin
August 17, 2013 ×

Very nice article :)It helped me a lot.One stop Answer for all the doubts in my head ..Thanks a lot for sharing ...:) :)

Reply
avatar
August 17, 2013 ×

Thanks manasa..stay tuned and stay connected for more updates..

Reply
avatar
August 19, 2013 ×

Hi.
Excelent article. I was very useful to understand this elements. Thank you very much!

Reply
avatar
August 19, 2013 ×

thanks javier for your appreciation..stay connected and keep reading.

Reply
avatar
Anonymous
admin
September 04, 2013 ×

good post sir..help me a lot

Reply
avatar
September 04, 2013 ×

thanks for your appreciation..keep reading.

Reply
avatar
Unknown
admin
September 08, 2013 ×

who is best Response.Redirect or Server.Transfer ?

Reply
avatar
September 09, 2013 ×

hello abhimanyu..it depends upon requirement..Response.Redirect or Server.Transfer can be used based on the points mentioned above.

Reply
avatar
September 09, 2013 ×

Hi Lalit this article help me a lot... can you please also add server.execute. i am quite confused.

Reply
avatar
September 09, 2013 ×

Sure virender...i will write a post including server.execute

Reply
avatar
Unknown
admin
November 25, 2013 ×

Its nice Article we CAN NOT find these types of article in any other site. Your work is really appreciating. but sir in interview Q/A There was not Enough Content. I really respect your work. you are outstanding. Thanks. Sir

Reply
avatar
November 25, 2013 ×

Hello Faisal Hasan..i have focused on updating the interviw Question and answers section..Have you read the new article :
Asp.Net interview questions and answers with example for beginner, intermediate and experienced level
http://www.webcodeexpert.com/2013/11/aspnet-interview-questions-and-answers.html

Reply
avatar
Unknown
admin
November 30, 2013 ×

hello sir can you upload post related to chat

Reply
avatar
Unknown
admin
July 21, 2014 ×

Hello Sir Please post JQuery Zoom on mouse hover

Reply
avatar

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..