Wednesday, December 12, 2007

Server.Transfer VS Response.Redirect

Example: A webform1.aspx wants to send the user to webform2.aspx.

Response.Redirect
Response.Redirect method simply causes the client browser to move to another specified URL, as code below:
Response.Redirect(“webform2.aspx”)

When it is called, it will send a 302 (Object Moved) redirect header to the client browser, telling it that webform1.aspx has moved to webform2.aspx and then the browser will send a request to the server for webform2.aspx. When the browser receives responses from the server, it uses the header information to generate another HTTP request to the new URL.

For this method, the redirection happens at the client side, so it involves 2 round trips to the server: request the original page, and request for the redirected page.

When webform1.aspx is redirected to webform2.aspx, query string parameters in webform1.aspx are unavailable in webform2.aspx. If you want to pass value from webform1.aspx to webform2.aspx, you have to pass it either by query string parameters or store it in a Session object.

Since the client browser initiates another second request, so it is possible to redirect to an external site.


Server.Transfer
Server.Transfer method runs the execution for navigate from webform1.aspx to webform2.aspx on the server side, and sends the result to the client browser. When this method is been called, webform1.aspx terminates execution and the flow of control is transferred to webform2.aspx. The webform2.aspx still uses the response stream created by the webform1.aspx.

When you use this method to navigate between pages, Server.Transfer maintains the original URL on the browser, namely webform1.aspx, because the navigation occurs on the server side and the client browser remains unaware of the transfer. You can’t use Server.Transfer to send users to an external site since the execution is running on the server.

Server.Transfer has a second parameter called preserveForm. By default, it is set to false and does not pass the form data or the query string of the original page to the transferred page. But by set it to True, you can preserve them to the page that you are redirected to, as code below:
Server.Transfer(“webform2.aspx”, True)

For example, there is a textbox1 in webform1.aspx. With the preserveForm parameter set to True, you would be able to retrieve the value of textbox1 in webform2.aspx by referencing Request.Form(“textbox1”).

But be aware that when this method is been used, an error will occur in certain circumstances that attempting to transfer the form and query string values. The destination page uses the same response stream that was created by the original page, and therefore the hidden _VIEWSTATE field of the original page ends up on the second page, which may causes the ASP.NET machine authentication check (MAC) assumes that the ViewState of the new page has been modified on the client.

To resolve this problem:
1. Do not pass the second parameter (default is false) if it is unnecessary. When no query string or Form fields is been transferred to the destination page, ASP.NET does not run the MAC.
2. Set the enableViewstateMac property to True on the destination page, and then set it back to False.

0 comments:

 

Get paid for your opinions! Click on the banner above to join Planet Pulse. Its totally free to sign up, and you can earn UNLIMITED. Find out more by visiting PLANET PULSE.
Sign up for PayPal and start accepting credit card payments instantly. http://www.emailcashpro.com
July Code Blog Copyright © 2010 Blogger Template Designed by Bie Blogger Template