In this article, I am going to explain difference between Response.Redirect and Server.Transfer in detail. This is one of the frequently asked questions related to ASP.NET.
Response.Redirect
- Syntax
Response.Redirect("webpageURL.aspx");
- Response.Redirect is used to redirect current user’s page request (For example Page1.aspx) to another target page (Page2.aspx) of same server or some other server.
- When the web server receives a request for redirection using Response.Redirect, it sends a response header to the web browser (client) which instructs the web browser (client) to send a new target page request back to the server.
- In other words, Response.Redirect causes two request/response cycles (two round trips):
- First Cycle
- Request: Web browser (client) has requested page (Page1.aspx).
- Response: Response.Redirect sends HTTP status code 302 to Web browser along with the new URL location (Page2.aspx) of the target page.
- Second Cycle
- Request: Once web browser receives this code, it requests to open the new location (Page2.aspx) of the resource that was suggested by the server.
- Response: Web server returned Page2.aspx to Web browser.
- First Cycle
- Diagram
- Response.Redirect is slower in performance as it involves extra round trip.
- Response.Redirect can be used to redirect to .aspx and html pages of same server or some another server.
Response.Redirect("Page2.aspx"); Response.Redirect("Page2.html");
- Response.Redirect can be used to redirect a user to an external website or other server URL as well as URL on same server.
So we can use the Response.Redirect method to redirect users request to another page on same server like below:Response.Redirect("Page2.aspx");
Response.Redirect method to redirect users request to another page of different server like below:
- Response.Redirect changes the URL in the web browser thus URL can be bookmarked if necessary.
- Response.Redirect generates HTTP Status Code 302 (temporary redirect).
- By Response.Redirect, we can’t preserve all posted form variables or query string values of source page (Page1.aspx). All the Query Strings and Form variables are lost during the redirect and they are not available to the redirected (target : Page2.aspx) URL.
Response.Redirect("http://www.microsoft.com/myPage.aspx");
Server.Transfer
- Syntax
Server.Transfer (“webpageURL.aspx”);
- Server.Transfer is used to redirect current user’s page request (For example Page1.aspx) to another target page (Page2.aspx) of same server.
- When the web server receives a request for redirection using Server.Transfer, the original request is simply rewritten and transferred to some other page (Page2.aspx) on the same server.
- In other words, Server.Transfer causes only one request/response cycle (one round trip):
- Request: Web browser (client) has requested Page1.aspx. Server.Transfer transferred original request (Page1.aspx) to some other page (Page2.aspx) on the same server.
- Response: Web server returned Page2.aspx to Web browser.
- Diagram
- Server.Transfer is good in performance. Server.Transfer changes the "focus" on the Web server and transfers the request on some other page of same server. It doesn’t involve extra round trip which therefore reduces the load on Web server and makes applications run faster.
- Server.Transfer can be only used to transfer requestes to .aspx pages of same server.
- Using Server.Transfer, we cannot transfer request to external website url or some other server url.
- Server.Transfer doesn’t change the URL in the web browser thus Browser URL history doesn’t get updated. This makes confusion while debugging.
- All posted form variables and query strings can optionally remain available to the target Page (Page2.aspx). Server.Transfer is having overloaded method:
Server.Transfer(string urlPath, bool preserveForm;)
If you set "preserveForm" to true, using a statement such as Server.Transfer("Page2.aspx", true); the existing query string and any form variables of Page1.aspx will still be available to the Page2.aspx. By default, preserveForm is always true.
For example, If Page1.aspx has a TextBox control called "txtBox1" and Server.Transfer transferred Page1.aspx to Page2.aspx with the preserveForm parameter set to True then we would be able to retrieve value of the "txtBox1" of Page1.aspx by using "PreviousPage.FindControl" method on Page2.aspx.TextBox txtBox2 = (TextBox)this.Page.PreviousPage.FindControl("txtBox1");
What's up to all, the contents present at this site are truly awesome for people experience, well, keep up the good work
ReplyDeletefellows.
my weblog; simply click google.com