views:

968

answers:

4

Does Response.Redirect() cause the currently running method to abort? Or does code after Response.Redirect() execute also?

(That is, is it necessary to return/Exit Sub after a Response.Redirect?)

+5  A: 

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.

http://msdn.microsoft.com/en-us/library/a8wa7sdt(VS.80).aspx

David Stratton
+1  A: 

My understanding is that upon issuing a Response.Redirect(), code following it will not execute. If you think about it, it would make sense not to execute it. You're basically telling your code that you want to go somewhere else.

Example: Think of it as ordering a value meal at McDonalds. After you order it and they start filling your drink, you change your mind and say "you know what, forget my order. I'm going to Redirect myself to Wendy's." At that point, they're going to stop making your fries and burger because, well... you've decided to go somewhere else -- i.e. redirecting the response.

MunkiPhD
I like the use of a real-world example.
David Stratton
I would say that it was an analogy, not a real world example. A RWE would have been what happens inside the ASP.NET Framework and developer's code.
Colin Mackay
+5  A: 

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.

Martin Liversage
If I hadn't been 99% done writing my answer when you posted this I wouldn't have bothered with mine. Nice added detail about the ThreadAbortException too.
Jon
+1  A: 

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).

Jon