Asp.Net Page redirect

Response.Redirect(url,bln)
  • Makes round trip to browser.
  • The repsonse header contains a http status code of 302 (object moved), which directs browser to look into the header for the new url.
  • Can be used to transfer to external pages that are not running on the same server.
Server.Transfer(url,bln)
  • No round trip to browser. Bowser continues to show old url in its address bar
  • If bln == true, all the form variables will also be transferred to the new url.
  • Can be used to transfer to aspx pages running on the same server. Can not transfer to external websites.
  • Known issue: when bln is set to true.. click to open. The problem is, the _VIEWSTATE is also transferred to the new aspx page. This causes the ASP.NET machine authentication check (MAC) to assume that the ViewState of the new page has been tampered with. Solution is to set EnableViewStateMac to false in the new page.

Server.Execute (url,bln)

  • Works like a function call
  • If bln == true, all the form variables are transferred, so set EnableViewStateMac to false in the new page
  • Works for page in the same server.
  • Once the call is returned back, the response stream of the new page is added to the response stream of the called page. To have more control, the response of the new page can be fetched into a TextWriter and can be placed where ever required.

Sample code given below:

System.Text.StringWriter sw = new System.Text.StringWriter();
Server.Execute(“newPage.aspx”,sw);
lbl.Text = sw.ToString();