Server.Transfer navigates the pages within the same application or within the same server, the page is still in memory that can read the values directly from page2 on page1, in other words by using server.Transfer the page is not redirected permanently.
To demonstrate this, let us create one web application as in the following:
- "Start" - "All Programs" - "Microsoft Visual Studio 2010".
- "File" - "New WebSite" - "C#" - "Empty Web Site" (to avoid adding a master page).
- Provide the Project name such as "ResponseVsServer" or another as you wish and specify the location.
- Then right-click on Solution Explorer then seelct "Add New Item" - "Default.aspx" page (two web pages).
The UI will be look like as:
Now write the some code to redirect the pages as:
- using System;
- public partial class _Default : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- protected void Button1_Click(object sender, EventArgs e)
- {
- Response.Redirect("SecondPage.aspx");
- }
- protected void Button2_Click(object sender, EventArgs e)
- {
- Server.Transfer("SecondPage.aspx");
- }
- protected void Button3_Click(object sender, EventArgs e)
- {
- Response.Redirect("http://www.mcnsolutions.net");
- }
- protected void Button4_Click(object sender, EventArgs e)
- {
- Server.Transfer("http://www.mcnsolutions.net");
- }
- }
Now navigate the page using Response.Redirect within the same application; then the URL in the browser will be changed as:
Now in the preceding example we redirect the page using Response.Redirect. Then the page is permanently redirected on the second page and we can see the complete URL where the page is redirected to.
Now navigate the page using Server.Transfer within the same application; then the URL in the browser will not be changed, it remains the same as from where we redirected, but the processing of the second page will be done on the first page as:
In the preceding example we redirected the page using Server.Transfer. Then the page is not permanently redirected on the second page, but it processes the second page on a first page. As we clearly see, the URL is not changed but still it is on the first page and it shows the output of the second page on the first page.
Now navigate the page using Response.Redirect from one application to another application, it will be redirected.
Now navigate the page using Server.Transfer from one application to another application, it will not be redirected, it will give the following error:
It means that we cannot be redirected out of the server application using server.Transfer that is only used for navigating within the same application. In the preceding URL we can clearly see that the URL cannot be changed, in other words it is trying to process the page of a different server application on the same page but it's not possible; that’s why it gives Server Error.
Difference between Response.Redirect and Server.Transfer Response.Redirect
- The page is redirected permanently.
- Used to navigate within the same application as well as navigating from one application to another application.
- The URL is changed because the processing of the second page is done on the second page, not on the first page from where we redirected.
- We can bookmark the page because the full address is shown in a browser URL.
- An extra round trip happens to the server.
- It is used in HTML, ASP and ASP.Net pages to navigate from one page to another.
Server.Transfer
- The Page is not redirected permanently.
- Used to navigate within the same application, not outside of an application.
- The URL is changed because the processing of the second page is done on the same page, without navigating on the second page.
- We cannot bookmark the page because the full address is not shown in a browser URL.
- An extra round trip does not happen to the server; because of this it saves server resources.
- It is only used to navigate within an ASP or ASP.Net page, not within HTML pages.
Note
- Download the Zip file from the attachment for the full source code of an application.
Summary
I hope this article is useful for all readers, if you have any suggestion then please contact me including beginners also.