ASP.NET: What happens to code after Response.Redirect(...)? ASP.NET: What happens to code after Response.Redirect(...)? asp.net asp.net

ASP.NET: What happens to code after Response.Redirect(...)?


Response.Redirect has an overload accepting a boolean argument that indicates if the call to Response.Redirect should end the response. Calling the overload without this argument is the same as specifying true to indicate that the response should end.

Ending the reponse means that Response.End is called after the response has been modified to make the redirect happen, and Response.End throws an ThreadAbortException to terminate the current module.

Any code after a call to Response.Redirect is never called (unless you supply false for the extra argument). Actually, code in finally and certain catch handlers will execute, but you cannot swallow a ThreadAbortException.


This may not be a complete answer, but from what I've seen...

Response.Redirect does, actually cause the code to stop executing by throwing a System.Threading.ThreadAbortException.

You can see this for yourself by setting up global error handling in the Global.Asax and testing a Response.Redirect.

EDIT

and here is a link to the documentation that supports my answer:

Redirect calls End which raises a ThreadAbortException exception upon completion.

HttpResponse.Redirect Method (String, Boolean) (System.Web)


There is another parameter to Response.Redirect called endResponse. Setting it false is a good idea when you're redirecting in a try catch block because the context still needs control to be correct. So your catch block will pick up the exception.

The caveat to that is that when the page is not Cancelable then it won't try to get control. The most common case of this is Global.asax. So you don't need to worry about this exception in that context. If you don't believe me try reflecting the code for this method and take a look.

So to answer your question it isn't necessary to do much after a Response.Redirect when you set endResponse to true which it is by default (i.e. called with the method that doesn't take a bool).