Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
When should i use server.Transfer() and Response.Redirect() in Asp.net
 
What is the best programming Practices.
 

Thanks
Maruthi
Posted 19-May-11 0:30am
Edited 19-May-11 1:18am
v2
Comments
koolprasad2003 at 19-May-11 7:18am
   
Improve readability
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
 
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.
 
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.
 
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
  Permalink  
Comments
Albin Abel at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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")
  Permalink  
Comments
Deepthi Aravind at 19-May-11 7:39am
   
Then Wht abt query string????
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

This [^]will help you
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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'
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

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...
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 412
1 sanket saxena 355
2 Abhinav S 258
3 OriginalGriff 235
4 CPallini 205
0 Sergey Alexandrovich Kryukov 8,704
1 OriginalGriff 5,000
2 Peter Leow 3,839
3 Maciej Los 3,535
4 Er. Puneet Goel 3,107


Advertise | Privacy | Mobile
Web02 | 2.8.140415.2 | Last Updated 16 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid