Server.Transfer Vs. Response.Redirect

2018-12-31 18:52发布

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

  • What are advantages and disadvantages of each?
  • When is one appropriate over the other?
  • When is one not appropriate?

16条回答
弹指情弦暗扣
2楼-- · 2018-12-31 19:17

Just more details about Transfer(), it's actually is Server.Execute() + Response.End(), its source code is below (from Mono/.net 4.0):

public void Transfer (string path, bool preserveForm)
{
    this.Execute (path, null, preserveForm, true);
    this.context.Response.End ();
}

and for Execute(), what it is to run is the handler of the given path, see

ASP.NET does not verify that the current user is authorized to view the resource delivered by the Execute method. Although the ASP.NET authorization and authentication logic runs before the original resource handler is called, ASP.NET directly calls the handler indicated by the Execute method and does not rerun authentication and authorization logic for the new resource. If your application's security policy requires clients to have appropriate authorization to access the resource, the application should force reauthorization or provide a custom access-control mechanism.

You can force reauthorization by using the Redirect method instead of the Execute method. Redirect performs a client-side redirect in which the browser requests the new resource. Because this redirect is a new request entering the system, it is subjected to all the authentication and authorization logic of both Internet Information Services (IIS) and ASP.NET security policy.

-from MSDN

查看更多
残风、尘缘若梦
3楼-- · 2018-12-31 19:19

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.

查看更多
忆尘夕之涩
4楼-- · 2018-12-31 19:19

Response.Redirect Response.Redirect() will send you to a new page, update the address bar and add it to the Browser History. On your browser you can click back. It redirects the request to some plain HTML pages on our server or to some other web server. It causes additional roundtrips to the server on each request. It doesn’t preserve Query String and Form Variables from the original request. It enables to see the new redirected URL where it is redirected in the browser (and be able to bookmark it if it’s necessary). Response. Redirect simply sends a message down to the (HTTP 302) browser.

Server.Transfer Server.Transfer() does not change the address bar, we cannot hit back.One should use Server.Transfer() when he/she doesn’t want the user to see where he is going. Sometime on a "loading" type page. It transfers current page request to another .aspx page on the same server. It preserves server resources and avoids the unnecessary roundtrips to the server. It preserves Query String and Form Variables (optionally). It doesn’t show the real URL where it redirects the request in the users Web Browser. Server.Transfer happens without the browser knowing anything, the browser request a page, but the server returns the content of another.

查看更多
倾城一夜雪
5楼-- · 2018-12-31 19:20

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.

查看更多
登录 后发表回答