Server.Transfer Vs. Response.Redirect

前端 未结 16 2283
夕颜
夕颜 2020-11-22 14:38

What is difference between Server.Transfer and Response.Redirect?

  • What are advantages and disadvantages of each?
  • When is one
相关标签:
16条回答
  • 2020-11-22 15:37

    enter image description here

    "response.redirect" and "server.transfer" helps to transfer user from one page to other page while the page is executing. But the way they do this transfer / redirect is very different.

    In case you are visual guy and would like see demonstration rather than theory I would suggest to see the below facebook video which explains the difference in a more demonstrative way.

    https://www.facebook.com/photo.php?v=762186150488997

    The main difference between them is who does the transfer. In "response.redirect" the transfer is done by the browser while in "server.transfer" it’s done by the server. Let us try to understand this statement in a more detail manner.

    In "Server.Transfer" following is the sequence of how transfer happens:-

    1.User sends a request to an ASP.NET page. In the below figure the request is sent to "WebForm1" and we would like to navigate to "Webform2".

    2.Server starts executing "Webform1" and the life cycle of the page starts. But before the complete life cycle of the page is completed “Server.transfer” happens to "WebForm2".

    3."Webform2" page object is created, full page life cycle is executed and output HTML response is then sent to the browser.

    enter image description here

    While in "Response.Redirect" following is the sequence of events for navigation:-

    1.Client (browser) sends a request to a page. In the below figure the request is sent to "WebForm1" and we would like to navigate to "Webform2".

    2.Life cycle of "Webform1" starts executing. But in between of the life cycle "Response.Redirect" happens.

    3.Now rather than server doing a redirect , he sends a HTTP 302 command to the browser. This command tells the browser that he has to initiate a GET request to "Webform2.aspx" page.

    4.Browser interprets the 302 command and sends a GET request for "Webform2.aspx".

    enter image description here

    In other words "Server.Transfer" is executed by the server while "Response.Redirect" is executed by thr browser. "Response.Redirect" needs to two requests to do a redirect of the page.

    So when to use "Server.Transfer" and when to use "Response.Redirect" ?

    Use "Server.Transfer" when you want to navigate pages which reside on the same server, use "Response.Redirect" when you want to navigate between pages which resides on different server and domain.

    enter image description here

    Below is a summary table of which chalks out differences and in which scenario to use.

    enter image description here

    0 讨论(0)
  • 2020-11-22 15:37

    Transfer is entirely server-side. Client address bar stays constant. Some complexity about the transfer of context between requests. Flushing and restarting page handlers can be expensive so do your transfer early in the pipeline e.g. in an HttpModule during BeginRequest. Read the MSDN docs carefully, and test and understand the new values of HttpContext.Request - especially in Postback scenarios. We usually use Server.Transfer for error scenarios.

    Redirect terminates the request with a 302 status and client-side roundtrip response with and internally eats an exception (minor server perf hit - depends how many you do a day) Client then navigates to new address. Browser address bar & history updates etc. Client pays the cost of an extra roundtrip - cost varies depending on latency. In our business we redirect a lot we wrote our own module to avoid the exception cost.

    0 讨论(0)
  • 2020-11-22 15:39

    Response.Redirect involves an extra round trip and updates the address bar.

    Server.Transfer does not cause the address bar to change, the server responds to the request with content from another page

    e.g.

    Response.Redirect:-

    1. On the client the browser requests a page http://InitiallyRequestedPage.aspx
    2. On the server responds to the request with 302 passing the redirect address http://AnotherPage.aspx.
    3. On the client the browser makes a second request to the address http://AnotherPage.aspx.
    4. On the server responds with content from http://AnotherPage.aspx

    Server.Transfer:-

    1. On the client browser requests a page http://InitiallyRequestedPage.aspx
    2. On the server Server.Transfer to http://AnotherPage.aspx
    3. On the server the response is made to the request for http://InitiallyRequestedPage.aspx passing back content from http://AnotherPage.aspx

    Response.Redirect

    Pros:- RESTful - It changes the address bar, the address can be used to record changes of state inbetween requests.

    Cons:- Slow - There is an extra round-trip between the client and server. This can be expensive when there is substantial latency between the client and the server.

    Server.Transfer

    Pros:- Quick.

    Cons:- State lost - If you're using Server.Transfer to change the state of the application in response to post backs, if the page is then reloaded that state will be lost, as the address bar will be the same as it was on the first request.

    0 讨论(0)
  • 2020-11-22 15:40

    There are many differences as specified above. Apart from above all, there is one more difference. Response.Redirect() can be used to redirect user to any page which is not part of the application but Server.Transfer() can only be used to redirect user within the application.

    //This will work.
    Response.Redirect("http://www.google.com");
    
    //This will not work.
    Server.Transfer("http://www.google.com");
    
    0 讨论(0)
提交回复
热议问题