In this article we will discuss difference between Response.Redirect() and Server.Transfer() in ASP .NET. If you miss our previous article the please go through the link Upload Large files in ASP .NET using Generic Handler. In this article we will cover,
- Difference between Response.Redirect() and Server.Transfer()
- When we should use Response.Redirect() and when Server.Transfer()
Both Response.Redirect and Server.Transfer methods are used to transfer a user from one web page to another web page. Both methods are used for the same purpose but still there are some differences as follows.
➤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 round trips 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.
Fig-1 |
When we run the application it found that the HTTP status code is "HTTP 200 OK". Then you click on the button that redirects to another page using the Response.Redirect method. The Response.Redirect method first sends a request to the web browser so it is the "HTTP 302 Found" status, then the browser sends a request to the server and the server deliver a response so it is "HTTP 200 OK". It is called a round trip.
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.
Fig-2 |
➥ Code Snippet
Response.Redirect() can be used to redirect user to any page which is not part of the application but Server.Transfer() can only be used to redirect user within the application.
➤ In what condition we should use server.transfer instead of response.redirect
If you want to redirect a user from one server to another server you should use Response.Redirect, otherwise Server.Transfer would do. For example. If you want have a button click where you want to redirect it on to Google's website - here you can use Response.Redirect, because your are working in .Net environment with your own server and you are redirecting to some different server i.e. Google.
Summary
Post a Comment