I’m in the process of separating my articles on music from software blog entries and moving them to a different domain. So as part of this, I thought of doing permanent redirects for articles on my existing website from code.
I wrote a small piece of code to do a redirect. Below is the relevant piece of code that caused the issue.
However, I kept getting a ThreadAbortException no matter what I tried.
System.Threading.ThreadAbortException: Thread was being aborted. at System.Threading.Thread.AbortInternal() at System.Threading.Thread.Abort(Object stateInfo) at System.Web.HttpResponse.AbortCurrentThread() at System.Web.HttpResponse.End() at System.Web.HttpResponse.Redirect(String url, Boolean endResponse, Boolean permanent) at System.Web.HttpResponse.Redirect(String url)
As it turns out the root of the issue lies in the fact that Response.Redirect calls Response.End method internally which shifts execution the Application_EndRequest event in the event pipeline for the application. Due to this after the Response.Redirect call, the thread is aborted and the rest of the code is not executed.
We can avoid getting into this situation by using an overload of the above method as shown below.
Now, what the above call does is tells the method not to call Response.End, thereby not aborting the thread.
This exception can also occur with any method that internally calls Response.End such as Server.Transfer
You can read the KB article for more details: http://support.microsoft.com/kb/312629
Stackoverflow also has an interesting discussion revolving around this issue. http://stackoverflow.com/questions/2777105/response-redirect-causes-system-threading-threadabortexception