Response.Redirect throws an “Thread was being aborted”

Response.Redirect causes the browser to redirect to a different URL. It does so by sending a 302 – Object Moved to the browser, requesting it to do another roundtrip to the new page. Here is an example:

protected void Page_Load(object sender, EventArgs e)
    if (somethingIsTrue)
      Response.Redirect("", true);
  catch (Exception ex)
    // All exceptions are caught and written
    // to a log file

But here is something I didn’t know. When doing the Response.Redirect, .net will automatically throw an System.Threading.ThreadAbortException when the redirect is called. Apparently this is by design (although I’m nor sure I agree with the design).
The code above will write a log line for each time the Response.Redirect is called. This will flood my log file.

There are 4 solutions.

1) Set the EndResponse (the 2nd parameter) to false. The thread is aborted when the response is ended, so if you do not end the response, no abortion is done (this is untested, but according to several blogs valid).

2) Move the Response.Redirect outside the try/catch block.

3) Filter the exceptions. Do nothing if the ThreadAbortException occurs:

protected void Page_Load(object sender, EventArgs e)
    if (somethingIsTrue)
      Response.Redirect("", true);
  catch (ThreadAbortException ex1)
    // do nothing
  catch (Exception ex2)
    // All remaining exceptions are caught and written
    // to a log file

4) Do not use Response.Redirect. Instead, modify the response headers (this is the rather stupid solution, but I have added it to show that you can redirect without using Response.Redirect):

Response.Status = "302 Object Moved";
Response.RedirectLocation = "";

About briancaos

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in .net, c# and tagged , , , , . Bookmark the permalink.

5 Responses to Response.Redirect throws an “Thread was being aborted”

  1. Mark Cassidy says:

    As to your 4) solution, that won’t actually help. The TAException is being raised by Response.End() which implicitly is called by Response.Redirect( “site”, true ) when instructed to end the response. Reference:

    That being said, I think we could all do well and consider if what we mean isn’t actually a Server.Transfer situation. I know I myself often need to send the user elsewhere on my site, and I routinely use Response.Redirect to do so. I know I shouldn’t, but I can’t get rid of all my old Classic ASP bad habits… ;-)

  2. I was handling threadabortexception today and I found this
    wonderful article I
    think catching the threadabortexception is the best solution to
    handle it. By the way we always use sitecore.web.webutil.redirect
    in stead of response.redirect

  3. MikeBeacon says:

    The reason for the error is to prevent all code past the redirect from processing – creating unexpected results. I suspect that they did this because of the behaviour difference from Classic ASP which stopped processing the current file when a response.redirect was called.

    If you want to stop a .VB sub/function without an error you can set EndResponse to false as listed above in item 1 and call response.end after each response.redirect.

  4. Jagmohan Kasana says:

    just try this Code ::::
    Response.Redirect(“abc.aspx”, False)

    i think it will help u…….bye bye take care

  5. Praveen says:

    Thank u very much…

    i’m searched for it long time…
    4th solution worked for me…. Really thankfull to u

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.