Tuesday, 18 September 2012

Difference between Response.Redirect(String) Vs Response.Redirect(String, Boolean) in ASP.NET

Response.Redirect(String) - Redirects a client to a new URL and specifies the new URL.
Response.Redirect(string url)

Response.Redirect(String, Boolean) - Redirects a client to a new URL. Specifies the new URL and whether execution of the current page should terminate.
Response.Redirect(string url, bool endResponse)

Response.Redirect(url, true) - The client will be sent the redirect for the new page and present page will immediately stop processing as a thread abort will occur.  This is the default behavior of a redirect.

Response.Redirect(url, false) – In this overload method, the second parameter tells the system whether to make the internal call to Response.End() or not. Parameter should be false to make the client is sent to redirect url but call to Response.End is skipped. This is one way to avoid the exception, but the cost is that this thread doesn’t stop executing the Application events!

If you are doing a redirect in Page_Init (or any page events) and call Response.Redirect(url, false) the page will only redirect once the current page is done executing.  This means that any server side processing you are performing on that page WILL get executed.

No comments:

Post a Comment