Saturday, December 11, 2010


Since I started ASP.NET I had always been in the confusion that what is the difference between Server.Transfer() and Response.Redirect(). Tried a lot but dint got succeed to find out the right description. And this thing gets more irritating when it comes to giving interviews. As I am an ASP.Net programmer interviewer expects I should know it, and I should have, However; if you search it over the internet you’ll find lots of posts same was the case with me but none of them solve my issue.

So I decide to write the post on it so that no one will do the donkey work which I did. I am writing this post for beginner and intermediate audience in ASP.Net.

Does not allow to navigate to url outside the current server and current site, it requires virtual path and if you try to do so following exception will occur

Invalid path for child request 'http://….'. A virtual path is expected
Allows navigate to the URL outside the current server.
Does not cause round trip (postback)
Cause round trip (postback)
Does not show the redirected URL. Means you are running a website at following URL.

You have got two web pages default.aspx, URL.aspx.

If you navigate from default.aspx to url.aspx, URL in address bar will still remains same even argument in query string will not be shown.

Explanation: When a user opens browser, type in the request in the address bar, and hit enter request goes to server and at the same time browser is also aware of the fact that what the URL is.

As we know that Server.Transfer() does not cause post back and directly redirects form the server side to the requested page that is why URL in the address bar does not update.

URL in the address bar change accordingly when using Response.Redirect().

It happens because of the fact that Response.Redirect() cause a post pack then redirects to the request page. When Response.resirect() cause a post back it takes the URL of the requested page to the client and then browser make a request the that URL.

In-order to update URL in the address bar browser requires complete cycle which starts from requesting URL from browser and sending HTML back to the browser.
Can only redirect to .aspx pages not to html pages.
Can redirect to .aspx pages and html pages.

When to Use?
We want to transfer current page request to another .aspx page on the same server
We want to redirect the request to some plain HTML pages on our server or to some other web server
We want to preserve server resources and avoid the unnecessary round trips to the server
We don't care about causing additional round trips to the server on each request
We want to preserve Query String and Form Variables (optionally)
We do not need to preserve Query String and Form Variables from the original request.
We don't need to show the real URL where we redirected the request in the users Web Browser
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 it’s necessary)

Hope this post will be help full for you people.

No comments:

Post a Comment