{{announcement.body}}
{{announcement.title}}

What Round Trip Means in Response.Redirect()

DZone 's Guide to

What Round Trip Means in Response.Redirect()

ASP.NET developers are familiar with this method, but what exactly is the "round trip" that is taken when the method is called? We investigate.

· Web Dev Zone ·
Free Resource

Every developer goes to an interview for future growth and the interviewer also asks the same basic questions, the developer provides the answer that they've read about and studied, but sometimes the interviewer changes the basic question in a tricky way.

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

  • Response.Redirect() is useful when we want to go to the page of external websites, whereas 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.
  • Response.Redirect() takes the round trip whereas 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?"

Answer: In the case of Response.Redirect(), first send the request to the Browser with the "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 what if I'm talking about Server.Transfer() that directly sends the request to the server and gets the response, "HTTP 200 Ok" from the server?

Let's check it out. 

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

WebSite

After creating the web site, I will create two pages named "Page1.aspx" and "Page2.aspx" for redirection from the first page to the second page.

Page1

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

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

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

Network

When I run the "page1.aspx" page it redirects meto "Page2.aspx." 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 file:

running the page1
As you can see in the above image, it went to page1.apsx with the "HTTP 302" code and then 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

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

Code
When I run the "page1.aspx" page it redirects me to "Page2.aspx." 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 ExplorerAfter running the page1.aspx file:

localhost
As you can see in the preceding image, the browser got the response from the server with the "HTTP 200" code without any round trips involved.

Reference

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

Topics:
web dev ,asp.net ,round trips ,asp.net tutorial in c#

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}