简体   繁体   中英

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

Server.TransferResponse.Redirect什么区别?

Response.Redirect sends a '302 Moved Temporarily' response to the client, the client browser will then issue a request to that location.

Server.Transfer transfers the control from one page to the other on the server side, so the original Request and Response buffer streams remain as they are at the point the transfer is done.

This means that Response.Redirect requires a round trip to the client but a Server.Transfer does not. The other difference is that the Server.Transfer appears to the browser as the original url... eg consider Page1.aspx does a server.transfer to page2.aspx, In this case Page1.aspx shows in the address bar even though they are actually being shown Page2.aspx. If instead Page1.aspx did a Response.Redirect then Page2.aspx would be shown.

So depending on what you want to optimise Response.Redirect are better if you want to support bookmarking of pages correctly and Server.Transfers are better if you want to minimise client round trips to the server.

Oh, and do check out http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=15 it describes this much better with the caveats.

Server.Transfer() only works with pages from your site, it means that the server starts rendering a new page from scratch.

Response.Redirect() is a normal redirection, which works with any URL.

Response.Redirect should be used :

  • if we want to redirect the request to same server or to some other web server
  • if we don't care about causing additional roundtrips to the server on each request

  • if we do not need to preserve Query String and Form Variables from the original request

  • if we want our users to be able to see the new redirected URL where he is redirected in his browser

  • Response.Redirect is more user-friendly, as the site visitor can bookmark the page that they are redirected to.

    Server.Transfer should be used :


  • if we want to transfer current page request to another .aspx page on the same server not on the another server
    Example if your server is www.yahoo.com you cannot use server. Transfer to move to www.microsoft.com
  • if we want to preserve server resources and avoid the unnecessary roundtrips to the server
  • if we want to preserve Query String and Form Variables -if we don't need to show the real URL where we redirected the request in the users Web Browser

Server.Transfer为您提供了将参数值设置为目标页面的选项。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM