Friday, July 20, 2012

“Server.Transfer()” vs “Response.Redirect()”

Both “Server.Transfer()” and “Response.Redirect()” are used to transfer a user from one page to another page. Logically both methods are used for the same purpose but still there are some technical differences between both which we need to understand.

The basic difference between them is the way they communicate. Response.Redirect() first sends request for new page to the browser, then browser sends that request to the web-server, and after that your page changes. But Server.Transfer() directly communicate with the server to change the page hence it saves an extra  round-trip (to the browser) in the whole process.

Now the question arises which to use and when to use?

Response.Redirect() should be used when:
  •  we want to redirect the request to some plain HTML pages on our server or to some other web server
  •  we don't care about causing additional roundtrips to the server on each request
  • we do not need to preserve Query String and Form Variables from the original request
  • 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)

Server.Transfer() should be used when:
  • we want to transfer current page request to another .aspx page on the same server
  • we want to preserve server resources and avoid the unnecessary roundtrips to the server
  • we want to preserve Query String and Form Variables (optionally)
  • we don't need to show the real URL where we redirected the request in the users Web Browser

No comments:

Post a Comment