Friday, September 10, 2010

Request.Redirect vs. Server.Transfer in ASP.NET

Which is "better" -- Request.Redirect or Server.Transfer? Both provide similar functionality, which is to send the viewer/browser to another webpage. But they way they achieve the task is slightly different. With Request.Redirect, the server basically goes back and tell the browser to go to a different page instead. With Server.Transfer, the server just returns a different page without notifying the browser. So the browser is not any wiser that it has already been sent to a different page. The url in its navigation bar stays the same (which is that of the original url visited).

Performance-wise, there is a difference. Because Request.Redirect is a request to the browser to go to another page, it involves an extra roundtrip. Depending on how fast or slow your Internet connection is, this extra trip can add a pause of a second or two while the browser fetch the new page. With Server.Transfer, the redirected page appears immediately in lieu of the originally requested page.

I have to admit, I'm beginning to be a bit of a fan of Server.Transfer. But most programmers don't seem to like it based on other blog posts. They say that its an old API that should not be used anymore. One of its disadvantages is that it will not allow the user/browser to bookmark a page since it does not know the actual url of the redirected page.

With .NET, passing parameters from one webpage to another using Server.Transfer requires the use of the Context objects. With Request.Redirect, the practice is to just use the more common Session object. Truth be told, I really don't understand what is the difference between those two object types. PayEasy uses a combination of both.

No comments: