Sunday 20 October 2013

Difference between Response.Redirect and Server.Transfer in ASP.NET

1 comment
In this article, I am going to explain difference between Response.Redirect and Server.Transfer in detail. This is one of the frequently asked questions related to ASP.NET

Response.Redirect

  1. Syntax
    Response.Redirect("webpageURL.aspx");
    
  2. Response.Redirect is used to redirect current user’s page request (For example Page1.aspx) to another target page (Page2.aspx) of same server or some other server.
  3. When the web server receives a request for redirection using Response.Redirect, it sends a response header to the web browser (client) which instructs the web browser (client) to send a new target page request back to the server.
  4. In other words, Response.Redirect causes two request/response cycles (two round trips):
    1. First Cycle
      • Request: Web browser (client) has requested page (Page1.aspx).
      • Response: Response.Redirect sends HTTP status code 302 to Web browser along with the new URL location (Page2.aspx) of the target page.
    2. Second Cycle
      • Request: Once web browser receives this code, it requests to open the new location (Page2.aspx) of the resource that was suggested by the server.
      • Response: Web server returned Page2.aspx to Web browser.
  5. Diagram
  6. Response.Redirect is slower in performance as it involves extra round trip.
  7. Response.Redirect can be used to redirect to .aspx and html pages of same server or some another server.
    Response.Redirect("Page2.aspx");
    Response.Redirect("Page2.html");
    
  8. Response.Redirect can be used to redirect a user to an external website or other server URL as well as URL on same server.
    So we can use the Response.Redirect method to redirect users request to another page on same server like below:
    Response.Redirect("Page2.aspx");
    
  9. Response.Redirect method to redirect users request to another page of different server like below:
    Response.Redirect("http://www.microsoft.com/myPage.aspx");
    
  10. Response.Redirect changes the URL in the web browser thus URL can be bookmarked if necessary.
  11. Response.Redirect generates HTTP Status Code 302 (temporary redirect).
  12. By Response.Redirect, we can’t preserve all posted form variables or query string values of source page (Page1.aspx). All the Query Strings and Form variables are lost during the redirect and they are not available to the redirected (target : Page2.aspx) URL.

Server.Transfer

  1. Syntax
    Server.Transfer (“webpageURL.aspx”);
    
  2. Server.Transfer is used to redirect current user’s page request (For example Page1.aspx) to another target page (Page2.aspx) of same server.
  3. When the web server receives a request for redirection using Server.Transfer, the original request is simply rewritten and transferred to some other page (Page2.aspx) on the same server.
  4. In other words, Server.Transfer causes only one request/response cycle (one round trip):
    • Request: Web browser (client) has requested Page1.aspx. Server.Transfer transferred original request (Page1.aspx) to some other page (Page2.aspx) on the same server.
    • Response: Web server returned Page2.aspx to Web browser.
  5. Diagram

  6. Server.Transfer is good in performance. Server.Transfer changes the "focus" on the Web server and transfers the request on some other page of same server. It doesn’t involve extra round trip which therefore reduces the load on Web server and makes applications run faster. 
  7. Server.Transfer can be only used to transfer requestes to .aspx pages of same server.
  8. Using Server.Transfer, we cannot transfer request to external website url or some other server url.
  9. Server.Transfer doesn’t change the URL in the web browser thus Browser URL history doesn’t get updated. This makes confusion while debugging.
  10. All posted form variables and query strings can optionally remain available to the target Page (Page2.aspx). Server.Transfer is having overloaded method:
    Server.Transfer(string urlPath, bool preserveForm;)
    
    If you set "preserveForm" to true, using a statement such as Server.Transfer("Page2.aspx", true); the existing query string and any form variables of Page1.aspx will still be available to the Page2.aspx. By default, preserveForm is always true.

    For example, If Page1.aspx has a TextBox control called "txtBox1" and Server.Transfer transferred Page1.aspx to Page2.aspx with the preserveForm parameter set to True then we would be able to retrieve value of the "txtBox1" of Page1.aspx by using  "PreviousPage.FindControl" method on Page2.aspx.
    TextBox txtBox2 = (TextBox)this.Page.PreviousPage.FindControl("txtBox1");
    
    If we set preserveForm=true to preserve query string and form variables then we may receive error: "The View State is invalid for this page and might be corrupted". This problem occurs because the EnableViewStateMac attribute of the <pages> element is set to true by default. For more detail on this error, visit PRB: "View State Is Invalid" Error Message When You Use Server.Transfer

    The workaround of this error is to set the enableViewStateMac property to false on the target page.
Comments and suggestions are most welcome. Happy coding!

1 comment :

  1. What's up to all, the contents present at this site are truly awesome for people experience, well, keep up the good work
    fellows.

    my weblog; simply click google.com

    ReplyDelete