Difference Between Server.Transfer and Response.Redirect (in ASP.NET)

 Server.Transfer and Response.Redirect Method

There are so many discussions of the Response.Redirect and Server.Transfer methods of ASP.NET. I will show you in this article how they work programmatically or practically.

We can switch or move from one page to another page using both methods, but both methods have many differences. The differences you will see later but first I am showing you how to use both of them. I am simply creating an application for showing how to use both of them. Use the following procedure to create the sample application.

  1. First of all open the Visual Studio and create a new empty website.
  2. Now I am adding two aspx pages or ASP.NET Web Forms named "FirstPage.aspx" and "SecondPage.aspx".

    To add both pages go to the Solution Explorer and right-click on the solution and click on "Add" >> "Add new Item".

    Such as the following:

    Add New Item in Visual Studio


    Add New Item in visual studio

    And now select "Web Form" and give them a name of the page. I used the names "FirstPage.aspx" and "SecondPage.aspx." as in the following:

    Add Web Form in visual studio

  3. Now create two buttons (one for the Server.Transfer method and another one for the Response.Redirect method) using ToolBox as shown below.

    Server.Transfer vs Response.Redirect

    You can create a button using the following code also:
    1. <asp:Button ID="Button1" runat="server" Text="Server.Transfer Button" />  
    2. <asp:Button ID="Button2" runat="server" Text="Response.Redirect Button" />
  4. Now go to the design part and double-click on the Server.Transfer Button, use the following code for this button's click event.
    1. protected void Button1_Click(object sender, EventArgs e)  
    2. {  
    3.    Server.Transfer("SecondPage.aspx");  
    4. }
  5. Now also do the same for the Response.Redirect Button but use the following method for it:
    1. protected void Button2_Click(object sender, EventArgs e)  
    2. {  
    3.    Response.Redirect("SecondPage.aspx");  
    4. }
  6. Now build the project and run it.

    After running you will see the output looks such as the following:

    Now when you click on the Server.Transfer Button you will see output like:

    And when you will click on the Response.Redirect button you will see output like:

What you are seeing here is both output is the same, there is no difference in both of them.

So now we will see what the difference is between them.

So first I am describing Response.Redirect.

Response.Redirect: This method works like this. When the page is loaded, in other words when you send the request for the page, FirstPage.aspx, what it will do is go to the server and depending on the request you will get a response of the server and you will get "FirstPage.aspx".

Response.Redirect process

Now when you click on the Response.Redirect Button then the click event will fire on the server. That means the Post-Back will occur and the response occurs again for the same page, in other words "FirstPage.aspx".

Post-Back process

Now the client system takes the URL and returns to the server to find the page and we move to the next page, "SecondPage.aspx".

NOTE: After doing this just see the URL only; the URL has changed.

Server.Transfer: Now I will show you how Server.Transfer actually works.

First the server takes the request of the specific page and shows that page (the same as above).

Then after the button is clicked it will post-back the request to the server. At that time the server will directly transfer to that specific page, compile that page code and send it back to the client.

NOTE: After doing this just see the URL only; the URL will not change.

Simply if we see the difference using the URL then when we use the Response.Redirect Method the URL Changes. Look at the following:

Response.Redirect Method Change the URL

But when we use the Server.Transfer Method the URL doesn't change. Look below.

In Server.Transfer URL doesn't change

One of the most important differences is that the Server.Transfer method can only work when the page is at the same server, but the Response.Redirect method can be transfered when the server is at a different place.

For Example: If you use Server.Transfer such as follows:

And run it, then after clicking the Server.Transfer Button one error occurs, as in the following:

But when you do this with Response.Redirect you will be moved to the C# Corner website.

For Example: You write such as follows:

After writing this if you run the application and click on the Response.Redirect Button then the output will be:

So how Response.Redirect works is that it returns the request to the client after the redirect.

This is the main difference between Response.Redirect and Server.Transfer.