Introduction:
In ASP.NET there are two methods to redirect a user from one page to another page. One is using Response.Redirect() and other using Server.Transfer(). Even though both these methods do the same task, there is some difference between them and are meant to be used in different scenarios.
Both Response.Redirect and Server.Transfer works fine and redirects the user to the desired page but technically speaking the way they do the transfer / redirect is quiet different.
In this brief article we can have a look at how these methods differ and in which scenarios we should use each of them.
Response.Redirect Method:
Suppose you have two pages in your ASP.NET application say Page1.aspx and Page2.aspx. When you use Response.Redirect following are the sequence of actions that happen behind the scene to redirect the user from Page1.aspx to Page2.aspx.
This method can be used for both .aspx and HTML pages and this method should only be used if you want to redirect the user to an external page.
Server.Transfer Method:
When you use Server.Transfer following are the sequence of events for navigation:
In this case you can see that the URL in the browser address bar remains the same even after loading Page2.aspx page. Here the previous page also exists in server memory and so you can get data from the previous page to the new page by the Context.Item collection. Server.Transfer can be used only for .aspx pages and is specific to ASP and ASP.NET and this method cannot redirect the user to an external page.
Usage Scenario:
In ASP.NET there are two methods to redirect a user from one page to another page. One is using Response.Redirect() and other using Server.Transfer(). Even though both these methods do the same task, there is some difference between them and are meant to be used in different scenarios.
Both Response.Redirect and Server.Transfer works fine and redirects the user to the desired page but technically speaking the way they do the transfer / redirect is quiet different.
In this brief article we can have a look at how these methods differ and in which scenarios we should use each of them.
Response.Redirect Method:
Suppose you have two pages in your ASP.NET application say Page1.aspx and Page2.aspx. When you use Response.Redirect following are the sequence of actions that happen behind the scene to redirect the user from Page1.aspx to Page2.aspx.
- When the Page1.aspx page is accessed, the corresponding Page Life Cycle events get executed and in between the execution, the Response.Redirect happens.
- Immediately the server sends a HTTP 302 command to the browser (instructing the browser to initiate a GET request to Page2.aspx page).
- Browser interprets the 302 command and sends a GET request for Page2.aspx page. So here the page transfer is actually done by the browser.
This method can be used for both .aspx and HTML pages and this method should only be used if you want to redirect the user to an external page.
Server.Transfer Method:
When you use Server.Transfer following are the sequence of events for navigation:
- Server starts processing Page1.aspx page and the corresponding Page Life Cycle events are executed. But before the Page Life Cycle is completed Server.Transfer happens to Page2.aspx page.
- Here the page transfer is actually done by the server. So Page2.aspx page object is created, full page life cycle is executed and output HTML response is then sent to the browser.
In this case you can see that the URL in the browser address bar remains the same even after loading Page2.aspx page. Here the previous page also exists in server memory and so you can get data from the previous page to the new page by the Context.Item collection. Server.Transfer can be used only for .aspx pages and is specific to ASP and ASP.NET and this method cannot redirect the user to an external page.
Usage Scenario:
- You should use Response.Redirect when you want to redirect between pages which reside on different server and domain. Server.Transfer can be used when you want to redirect to a page which resides on the same server.
- If you want to perform a quick redirect then go for Server.Transfer (however it can be a little misleading to the user since the Url doesn't physically change). For example Server.Transfer is advisable for wizard style input forms split over multiple pages where the physical change of Url is not of much significance as you move from one form to another form. Server.Transfer conserves server resources. Instead of telling the browser to redirect, it simply changes the focus on the Web server and transfers the request. This means you don't get quite as many HTTP requests coming through, which therefore eases the pressure on your Web server and makes your applications run faster. On the other side Response.Redirect involves an extra round-trip between the client and the server. This extra round-trip can be expensive when there is substantial latency between the client and the server.
- Authentication and Force Re-authorization: In Server.Transfer, the Transfer() is actually Server.Execute() + Response.End() and for Execute() what it is to run is the handler of the given path. Here the issue is: ASP.NET does not verify that the current user is authorized to view the resource delivered by the Execute method. Although the ASP.NET authorization and authentication logic runs before the original resource handler is called, ASP.NET directly calls the handler indicated by the Execute method and does not rerun authentication and authorization logic for the new resource. If your application's security policy requires clients to have appropriate authorization to access the resource, the application should force re-authorization or provide a custom access-control mechanism. In Response.Redirect, You can force re-authorization by using the Redirect method instead of the Execute method. Redirect performs a client-side redirect in which the browser requests the new resource. Since this redirect is a new request entering the system, it is subjected to all the authentication and authorization logic of both Internet Information Services (IIS) and ASP.NET security policy. So Response.Redirect is more advisable from a security perspective.

