Server.Transfer Vs. Response.Redirect

前端 未结 16 2319
夕颜
夕颜 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:14

    The beauty of Server.Transfer is what you can do with it:

    TextBox myTxt = (TextBox)this.Page.PreviousPage.FindControl("TextBoxID");
    

    You can get anything from your previous page using the above method as long as you use Server.Transfer but not Response.Redirect

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

    Response.Redirect: tells the browser that the requested page can be found at a new location. The browser then initiates another request to the new page loading its contents in the browser. This results in two requests by the browser.

    Server.Transfer: It transfers execution from the first page to the second page on the server. As far as the browser client is concerned, it made one request and the initial page is the one responding with content. The benefit of this approach is one less round trip to the server from the client browser. Also, any posted form variables and query string parameters are available to the second page as well.

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

    Response.Redirect redirects page to another page after first page arrives to client. So client knows the redirection.

    Server.Transfer quits current execution of the page. Client does not know the redirection. It allows you to transfer the query string and form variables.

    So it depends to your needs to choose which is better.

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

    Response.Redirect simply sends a message (HTTP 302) down to the browser.

    Server.Transfer happens without the browser knowing anything, the browser request a page, but the server returns the content of another.

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

    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
    0 讨论(0)
  • 2020-11-22 15:19

    In addition to ScarletGarden's comment, you also need to consider the impact of search engines and your redirect. Has this page moved permanently? Temporarily? It makes a difference.

    see: Response.Redirect vs. "301 Moved Permanently":

    We've all used Response.Redirect at one time or another. It's the quick and easy way to get visitors pointed in the right direction if they somehow end up in the wrong place. But did you know that Response.Redirect sends an HTTP response status code of "302 Found" when you might really want to send "301 Moved Permanently"?

    The distinction seems small, but in certain cases it can actually make a big difference. For example, if you use a "301 Moved Permanently" response code, most search engines will remove the outdated link from their index and replace it with the new one. If you use "302 Found", they'll continue returning to the old page...

    0 讨论(0)
提交回复
热议问题