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
?
Response.Redirect()
throws an exception to abort the current request.This KB article describes this behavior (also for the
Request.End()
andServer.Transfer()
methods).For
Response.Redirect()
there exists an overload: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.
I had that problem too. Try using Server.Transfer instead of Response.Redirect Worked for me
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:
This blog post from Thomas Marquardt provides additional details, including how to handle the special case of redirecting inside an Application_Error handler.
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 Is Response.End() considered harmful?
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)
What I do is catch this exception, together with another possible exceptions. Hope this help someone.