Pages

Monday, 23 April 2012

Response.Redirect vs Server.Transfer: What to use when? An 11 point comparision

1. Response.Redirect simply sends a message to the browser, directing it to move to another page.

Example:

Response.Redirect("default.aspx");
or
Response.Redirect("
http://www.dzone.com");

On the other hand, Server.Transfer does not initiate another request to the server, but the original request is simply rewritten and transfered to some other page on the same server.

Example: Sever.Transfer("default.aspx");

2. Server.Transfer changes the "focus" on the Webserver 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. On the other hand, Response.Redirect will instruct browser to call a particular webpage.This will increase one request and one response between the client and server. This extra round-trip is often inefficient and unnecessary.

3. Server.Transfer maintains the original URL in the browser but Response.Redirect changes the URL.

4. The Server.Transfer method also has a second parameter—"preserveForm". If you set this to True, using a statement such as Server.Transfer("default.aspx", True), the existing query string and any form variables will still be available to the page you are transferring to. By default, this variable is always true.

For example, if your default.aspx has a TextBox control called "TextBox1" and you transferred to default2.aspx with the preserveForm parameter set to True, you would be able to retrieve value of the "TextBox1" of default.aspx by using "PreviousPage.FindControl" method.
But in case of Respons.Redirect, by the time default2.aspx is requested, default.aspx is flushed from the server’s memory and no information can be retrieved about it unless the developer explicitly saved the information using some technique like session, cookie, application, cache etc.

5. By Response.Redirect, we can navigate to another page in same site as well as different site, but by Server.Transfer we can only navigate the page that exists in same website.

6. Server .Transfer will also cause confusion when user refreshes the page. If user is on Page1 and by using Server.Transfer, user sees the Page2. The url on browser is still of Page1. So, if user refreshes the page, Page1 will be refreshed instead of Page2. This problem will never appear in case of Response.Redirect.

7. Server.Transfer may cause confusion while debugging as the URL does not change unlike Response.Redirect.

8. Response.Redirect is used like GET method in which we can see all information or address where we will be go. Server.Transfer is used like POST method in which one cannot see full address.

9. 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.

10. In case of Server.Transfer the browser's history is not updated but in Response.Redirect the browser's history is updated.

11. Bookmarking is ambiguous in case of Server.Transfer while in Response.Redirect, user can clearly bookmark a page.

Summary: What should be used where?

Response.Redirect should be used when:

1. We want to redirect the request to some plain HTML pages on our server or to some other web server.
2. We don't care about causing additional roundtrips to the server on each request.
3. We do not need to preserve Query String and Form Variables from the original request.
4. We want our users to be able to see the new redirected URL where he is redirected in his browser (and be able to
bookmark it if its necessary)

Server.Transfer should be used when:

1. We want to transfer current page request to another .aspx page on the same server.
2. We want to preserve server resources and avoid the unnecessary roundtrips to the server.
3. We want to preserve Query String and Form Variables.
4. We don't need to show the real URL where we redirected the request in the users Web Browser
 

No comments:

Post a Comment

About the Author

I have more than 10 years of experience in IT industry. Linkedin Profile

I am currently messing up with neural networks in deep learning. I am learning Python, TensorFlow and Keras.

Author: I am an author of a book on deep learning.

Quiz: I run an online quiz on machine learning and deep learning.