Response.Redirect and Server.Transfer in ASP.Net


Response.Redirect(url) and Server.Transfer(path) both let you to jump on another page which may be available on same server or may be on different server. Here we will see which one is to be used when and why.

Server.Transfer-Response.Redirect.bmp

Response.Redirect: (2 Trips)

A client (browser) sends a request to a server to display a Page1.aspx (Request1), the server starts executing code for Page1.aspx. As the code reaches the line:
Response.Redirect("Page2.aspx");

> Execution of Page1.aspx stops and a response is sent to the browser to make a request for Page2.aspx (Response1).
> The browser URL changes to http://...../Page2.aspx and the browser sends a request to Page2.aspx (Request2).
> The new server (that can be the same server) executes Page2.aspx and sends the response to the browser; Page2.aspx is displayed (Response2).

Server.Transfer (1 Trip)

A client (browser) sends a request to a server to display Page1.aspx (Request1), the server starts executing code for Page1.aspx. As the code reaches the line:

Server.Transfer("Page2.aspx");

> Execution of Page1.aspx stops and no response to the browser is sent but the server itself calls Page2.aspx.
>The server executes Page2.aspx (that is on the same server) and sends the content of Page2.aspx to the browser. 
>The browser is unaware that the server has moved it to Page1.aspx to Page2.aspx and it displays the received content so the URL in the browser doesn't changed. (Response1)
>The values of the controls can be retained in the Page1.aspx by passing an extra parameter as True i.e Server.Transfer("Page2.aspx", True). 

Response.Redirect Vs Server. Transfer

1- Response.Redirect makes 2 trips, Server.Transfer serves the same from 1 trip.

2- Response.Redirect can serve the page from various servers or from the same server because it makes 2 requests. Server.Transfer can serve the page from the same server only.

3- Response.Redirect can't retain the value of an old page. Server.Transfer can retain old page values.

4- Response.Redirect changes the URL since it makes a fresh request. Server.Transfer doesn't change the URL because the browser isn't aware of the transfer.

5- Use Response.Redirect to switch to another server, For example, if you are jumping to Yahoo.com from your application then use Response.Redirect. Use server.transfer if you want to jump to another page of the same application.

6- Response.Redirect can be used for both .aspx and HTML pages whereas Server.Transfer can be used only for .aspx pages and is specific to ASP and ASP.NET.

7- The Server.Transfer method is preferable to be used because Server.Transfer is faster since there is one less roundtrip, but some people say that Server.Transfer is not recommended since the operations typically flow through several pages, causing a loss of the correct URL of the page, but again it all depends on your requirements.

 Server.Transfer-Response.Redirect.bmp


Similar Articles