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".
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:
Open
Visual Studio 2015 and select "File" -> "New" -> "WebSite: and fill in the WebSite name as "WebSite1".
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.
For the identification, I have added some text on both pages as in the following.
Page1.aspx
Page2.aspxNow 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");
- }
- }
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.
After running the page1.aspx:
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
- public partial class Page1 : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- Server.Transfer("Page2.aspx");
- }
- }
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.
After running the page1.aspx:
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