Monday 17 September 2012

difference between response.redirect and server.transfer in asp.net

We can transfer current users page request to another page with two methods:

    * Response.Redirect (HttpResponse.Redirect Method)
    * Server.Transfer (HttpServerUtility.Transfer Method)


Response.Redirect()

Response.Redirect method is by far the simplest method of redirecting processing from a source page to a different destination or target page in the same or even across different applications. When the web server receives a request for redirection, it sends a response header to the client that causes the client to send a new request back to the server. In other words, a redirect causes two request/response cycles: one for the original and one for the new redirected request.

The main difference is the posted previous page values can't be accessable. Here also the data can pass through server variables and query string. It simply redirect the page from one page to another.




















Example: Response.Redirect("Default.aspx")


Server.Transfer()


Instead of relying on the client to make a request to a new page, Server.Transfer is a server-side redirection technique that simply changes the code's "focus" on the web server to a new page. Server.Transfer is far more efficient than Response.Redirect when the target page resides on the same web server, because it avoids the extra roundtrip and uses only server resources for the redirection.

The main advantage of this transfer the first page to second page with better performance. The data can pass through variables, query string and also can retrive from the previous page control value.

















Example : Server.Transfer("example.aspx")




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 its 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


2 comments:

  1. Excellent explanation.Thanks!!!

    ReplyDelete
  2. This is a nice article which explain the same in detail

    http://www.codeproject.com/Articles/775221/Server-Transfer-VS-Response-Redirect-Simplified

    ReplyDelete