Skip to content

Other coding suggestions

mikepetersonccv edited this page Oct 12, 2012 · 2 revisions

Response.Redirect(url, [true]) quietly throws ThreadAbortExceptions

  • Whenever Response.Redirect(url) or Response.Redirect(url, true) is called, it quietly throws a ThreadAbortException. This is supposedly by design, but mostly for backwords compatibility with older versions of ASP.NET. If you've ever looked in your output window when debugging, you might notice a whole bunch of ThreadAbortExceptions. These are most likely due to using Response.Redirect(url, [true]). These ThreadAbortExceptions are ignored by the debugger by default, but they can be little annoying and there is a little bit of performance hit due to them. Here is how how Redirects should be written:

    Old Way:

{
  if (something)
  {  
    // redirect and end execution on this page (by throwing ThreadAbortException)
    Response.Redirect( "somenewpage.aspx", true );
  }
  
  Kung.Foo();
}

Better Way:

{
  if (something)
  {  
    // redirect nicely, complete the request, and break out of the method with a return
    Response.Redirect( "somenewpage.aspx", false );
    Context.ApplicationInstance.CompleteRequest();
    return;
  }
  
  Kung.Foo();
}
Clone this wiki locally