16 June 2010

ASP.NET 1.1 Response.Redirect Exception

If you were/are an ASP.NET 1.1 developer, this exception probably looks familiar to you:

System.Threading.ThreadAbortException: Thread was being aborted

This exception occurs when you call Response.Redirect() or Response.Transfer().  You are pretty much guaranteed an exception, when these calls are inside a Try block!  This is due to how the .NET 1.1 framework handles Try blocks.

This is how I understand the situation: the compiler puts try block code into a virtual method.  Like the Main() method, it returns a result code, and a result code is expected to be returned.  Response.Redirect() and Response.Transfer() cause the current thread to be aborted.  It the thread aborts while in the Try block, the virtual method is unable to return a result code.  There is doubtless some complications with handling Try block code as a virtual method, which is not conducive to stopping the thread.

Solution:
  1. You should include the optional, second variable in your code, whenever possible.  This second input parameter is a boolean value, indicating whether the page should finish rendering.  (I haven't encountered any situations where I do!)
  2. Take the redirect or transfer call out of the Try block.

No comments:

Post a Comment

Please provide details, when posting technical comments. If you find an error in sample code or have found bad information/misinformation in a post, please e-mail me details, so I can make corrections as quickly as possible.