tags:

views:

555

answers:

6

When I use Response.Redirect(...) to redirect my form to a new page I get the error:

A first chance exception of type 'System.Threading.ThreadAbortException' occurred in mscorlib.dll
An exception of type 'System.Threading.ThreadAbortException' occurred in mscorlib.dll but was not handled in user code

My understanding of this is that the error is being caused by the webserver aborting the remainder of the page the response.redirect was called on.

I know I can add a second parameter to Response.Redirect that is called endResponse. If I set endResponse to True I still get the error but if I set it to False then I do not. I am pretty sure though that that means the webserver is running the rest of the page I redirected away from. Which would seem to be inefficient to say the least. Is there a better way to do this? Something other than Response.Redirect or is there a way to force the old page to stop loading where I will not get a ThreadAbortException?

+1  A: 

Here's the official line on the problem (I couldn't find the latest, but I don't think the situation has changed for later versions of .net)

spender
A: 

This is just how Response.Redirect(url, true) works. It throws the ThreadAbortException to abort the thread. Just ignore that exception. (I presume it is some global error handler/logger where you see it?)

An interesting related discussion http://stackoverflow.com/questions/1087777/is-response-end-considered-harmful

Martin Smith
Aborting a thread seems like a really heavy handed way to deal with premature end of response. I find it strange that the framework wouldn't prefer to re-use the thread instead of spinning up a new one to take its place.
spender
Does sound odd when you put it like that!
Martin Smith
+1  A: 

Response.Redirect() throws an exception to abort the current request.

This KB article describes this behavior (also for the Request.End() and Server.Transfer() methods).

For Response.Redirect() there exists an overload:

Response.Redirect(String url, bool endResponse)

If you pass endResponse=false, then the exception is not thrown (but the runtime will continue processing the current request).

If endResponse=true (or if the other overload is used), the exception is thrown and the current request will immediately be terminated.

M4N
A: 

I had this problem all the time. It usually means that you had your response.redirect embedded in either a using statement or a try/catch block (at least in my experience). Along those same lines, it is throwing that because you called a redirect before the page was in a state where it could safely redirect the response (wrong page event, control initialization, etc). Have you identified which redirect is causing it and when? Posting the code around it might help.

TheCodeMonk
+6  A: 

The correct pattern is to call the Redirect overload with endResponse=false and make a call to tell the IIS pipeline that it should advance directly to the EndRequest stage once you return control:

Response.Redirect(url, false);
Context.ApplicationInstance.CompleteRequest();

This blog post from Thomas Marquardt provides additional details, including how to handle the special case of redirecting inside an Application_Error handler.

Joel Fillmore
A: 

I had that problem too. Try using Server.Transfer instead of Response.Redirect Worked for me

Cheers

Marko