Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When should i use server.Transfer() and Response.Redirect() in Asp.net

What is the best programming Practices.


Thanks
Maruthi
Posted
Updated 19-May-11 1:18am
v2
Comments
koolprasad2003 19-May-11 7:18am    
Improve readability

Response.Redirect, the redirection happens on the client browser, while Server.Transfer simply change focus between the pages in the same application. Response.Redirect postback and the life cycle of the redirecting page ends. Server.Transfer still keeps the previous page in memory and can be accessed through the previous page property.

Here is an example shows Server.Transfer still keeps the previous page in memory.

Have a simple interface

C#
public interface IMarker
{
    string TestString { get; set; }
}


At page1 implement this interface and set the value for the test string. At a button click event handler it transfers the page.

C#
public partial class _Default : System.Web.UI.Page,IMarker
{
    protected void Page_Load(object sender, EventArgs e)
    {
        TestString = "Hello world";
    }
    public string TestString { get; set; }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Server.Transfer("~/Default2.aspx");
    }
}


In the page 2 i.e Default2.aspx you can access this property which is still resides in memory.

C#
public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        IMarker page = (IMarker)this.PreviousPage;
        Response.Write(page.TestString);
    }
 }



Hope this explains how server tranfer works. Response redirect has to be used when simple redirection needs or redirect to a url which is not belong to this application. Response.Redirect goes a round trip page 1 to browser to page 2, where as transfer straightly move from page 1 to page to from the server side itself, but it still holds the page1 in memory. So choice is yours according to your requirement.

Good luck
 
Share this answer
 
Comments
Albin Abel 19-May-11 7:39am    
Forgot to add the server tansfer won't work properly with IIS7 integrated pipeline mode. You may need to use TransferRequest instead.
We used server.Transfer() when we need to send parameters with it like:

Server.Transfer("~/MemberProfile.aspx?UserID="+UserID.ToString())



but we used Response.Redirect() when there is no parameters needed:


Response.Redirect("~/MemberProfile.aspx")
 
Share this answer
 
Comments
Deepthi Aravind 19-May-11 7:39am    
Then Wht abt query string????
Response.Redirect()
can jump (navigates) from one server to another i.e., Response.Redirect("www.google.com");

but

Server.Transfer()
navigates within the same server only.

Response.Redirect()
takes many round trips so it is little bit slower in comparison with
Server.Transfer()


Hope this might clear your doubts.
 
Share this answer
 
v3
This [^]will help you
 
Share this answer
 
Here's a decent article on the differences

http://haacked.com/archive/2004/10/06/responseredirectverseservertransfer.aspx[^]

Notice that Response.Redirect sends an HTTP response '302 Object moved'
 
Share this answer
 
In the case of Response.Redirect there are two round trips
and in case of server.transfer there are only one roundtrip.

you can't use Server.Transfer to send the user to an external site. Only Response.Redirect can do that

One importance for server.trasfer is that you can access all control's data from another page
For eg:
Default1.aspx,
Contains 2 textboxes contains name and age
From this page you want to go Default2.aspx page
Server.transfer("Default2.aspx")


From Default2.aspx you can access the 2 textbox values from Default1.apsx

Simply we can access the previous page's information...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900