What Round Trip Means in Response.Redirect()

Content

Every developer goes to an interview for future growth and the interviewer also asks the some basic questions, the developer provides the answer that he reads and studies but sometimes the interviewer changes the basic question in a tricky way, like:

The interviewer might ask "What is the difference between Response.Redirect() and Server.Transfer()?". Then you will rely like:

  • Response.Redirect() is useful when we want to go to the page of external websites, where as Server.Transfer() is used for accessing the page of the internal websites or within the same server.

  • Response.Redirect() changes the URL but Server.Transfer() doesn't.

    And the following line for sure.

  • Response.Redirect() takes the round trip where as Server.Transfer() doesn't.
But what will happen when the interviewer asks you another question based on your answer.

"What does Round Trip mean, can you explain it and how can I see it visually".

                                          Round Trip

Answer: In the case of Response.Redirect(), first send the request to the Browser with "HTTP 302 Found" code then the Browser sends the request to the server and gets the response with "HTTP 200 Ok" from the server.

But if I talk about Server.Transfer() that directly sends the request to the server and gets the response with "HTTP 200 Ok" from the server.

To understand it visually see the following the code:

Start capturing

Open Visual Studio 2015 and select "File" -> "New" -> "WebSite: and fill in the WebSite name as "WebSite1".

WebSite

After creating the WebSite, I will create 2 Pages named "Page1.aspx" and "Page2.aspx" for re-direction from the first page to the second page.

Page1

For the identification, I have added some text on both pages as in the following.

Page1.aspx

Page1 aspx

Page2.aspx

Page 2 aspx

Now I will use both Response.Redirect() and Server.Transfer() on the "Page_Load" event of "Page1.aspx".

Response.Redirect()

It redirects to "Page2.aspx" from "Page1.aspx".

Code
  1. public partial class Page1 : System.Web.UI.Page  
  2. {  
  3.    protected void Page_Load(object sender, EventArgs e)  
  4.    {  
  5.       Response.Redirect("Page2.aspx");  
  6.    }  

Network

When I run the "page1.aspx" page it redirects to "Page2.aspx" and to check the existence of the round trip use the following procedure:
  • Open "Internet Explorer" and press "F12".
  • Click on the "Network" Tab.
  • Click on the "Start capturing" Button.

Start capturing and network

After running the page1.aspx:

running the page1

As you can see in the preceding image, where first it went to page1.apsx with the "HTTP 302" code and second got the response from the server with the "HTTP 200" code with the Round Trip.

Server.Transfer()

It redirects to "Page2.aspx" from "Page1.aspx".

Code

  1. public partial class Page1 : System.Web.UI.Page  
  2. {  
  3.    protected void Page_Load(object sender, EventArgs e)  
  4.    {  
  5.       Server.Transfer("Page2.aspx");  
  6.    }  

Code

When I run the "page1.aspx" page it redirects to "Page2.aspx" and to check the existence of the round trip use the following procedure:

  • Open "Internet Explorer" and press "F12".
  • Click on the "Network" Tab.
  • Click on the "Start capturing" Button.

Internet Explorer

After running the page1.aspx:

localhost

As you can see in the preceding image, where the browser got the response from the server with the "HTTP 200" code without any Round Trip.

Conclusion: Now you understand about the Round Trip in Response.Redirect().

 
Reference:  For HTTP Status Codes.
 
http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
 


Similar Articles