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

4

43

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

Cuirass answered 9/8, 2009 at 22:2 Comment(0)
T
29

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)

Theomancy answered 9/8, 2009 at 22:14 Comment(0)
H
56

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.

Howse answered 9/8, 2009 at 22:30 Comment(1)
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.Transport
T
29

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)

Theomancy answered 9/8, 2009 at 22:14 Comment(0)
T
3

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

Transport answered 9/8, 2009 at 22:32 Comment(0)
H
1

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.

Heuer answered 9/8, 2009 at 22:20 Comment(2)
I like the use of a real-world example.Theomancy
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.Keef

© 2022 - 2024 — McMap. All rights reserved.