21
NovDifference between Response.Redirect and Server.Transfer
Difference between Response.Redirect and Server.Transfer: An Overview
In ASP.Net Technology both "Server" and "Response" are objects of ASP.Net. Server.Transfer and Response.Redirect both are used to transfer a user from one page to another. But there is some remarkable differences between both the objects which are as follow. In this Tutorial, we will try to understand What is Response.Redirect?, What is Server.Transfer? and Difference between Response.Redirect and Server.Transfer. Consider enrolling in ASP.NET Certification Training for deeper understanding.
Read More: Top 50 ASP.NET Core Interview Questions and Answers for 2024
Difference between Response.Redirect and Server.Transfer
Response.Redirect and Server.Transfer, both are used to transfer the users from one page to another, but in different ways. Let's have a look at their differences through this comparison table:
Feature | Response.Redirect | Server.Transfer |
Purpose | With Response.Redirect, the user is redirected to a new page. | With Server.Transfer, the execution is transferred to a new page on the same server. |
Round Trips | It usually require two requests, that is, one to the original page and one to the redirected page. | It only requires a single request to the server. |
URL Change | The URL in the user's browser changes. | The URL remains the same. |
Performance | An additional round trip makes it slower. | It is comparatively faster. |
User awareness | The user is completely aware that they are being redirected to another page. | The user is unaware of the transfer as the URL in the browser does not change. |
Read More: How can you become an ASP.NET developer
Response.Redirect
Response.Redirect() will send you to a new page, update the address bar and add it to the Browser History. On your browser you can click back.
It redirects the request to some plain HTML pages on our server or to some other web server.
It causes additional roundtrips to the server on each request.
It doesn’t preserve Query String and Form Variables from the original request.
It enables to see the new redirected URL where it is redirected in the browser (and be able to bookmark it if it’s necessary).
Response. Redirect simply sends a message down to the (HTTP 302) browser.
Server.Transfer
Server.Transfer() does not change the address bar, we cannot hit back.One should use Server.Transfer() when he/she doesn’t want the user to see where he is going. Sometime on a "loading" type page.
It transfers current page request to another .aspx page on the same server.
It preserves server resources and avoids the unnecessary roundtrips to the server.
It preserves Query String and Form Variables (optionally).
It doesn’t show the real URL where it redirects the request in the users Web Browser.
Server.Transfer happens without the browser knowing anything, the browser request a page, but the server returns the content of another.
When to use "Server.Transfer" and when to use "Response.Redirect" ?
Use Server.Transfer:
- When you want to transfer the user to another page on the same server and you don't want the user to know about the transfer.
- When you want to save the data between pages. Use Context.Items collection or PreviousPage property.
- When you want it be quicker as it does not require another round trip to the user.
Use Response.Redirect:
- When you want to redirect the user to a whole new page that has a different URL.
- When you want that the user is made of them being redirected to another page as the new URL will be visible in the address bar.
- When you want the redirection to pages on totally different domains.
Summary
In this article I have tried to explain the difference between Response.Redirect and Server.Transfer also I explain some scenario when and where we should use each one. I hope the article will be helpful for you. Learn more about core concepts of ASP.NET and how to use them in real-time through our comprehensive step-by-step course ASP.NET Certification.
FAQs
- If you want a server-side navigation without revealing the new URL, use Server.Transfer method.
- If you want to redirect the user and it's okay for the URL change to be seen, use Response.Redirect method.