Vakul's .NET Realm

My journey towards .NET World…

  • Details

    Vakuls MCPD Certification Vakuls MCTS Certification Author's Pic



  • Blog Stats

    • 142,380 hits
  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 355 other followers

  • Follow me in Twitter:

    Error: Twitter did not respond. Please wait a few minutes and refresh this page.




  • qrcode
  • Subscribe

  • del.icio.us:

Response.Redirect(url) : Solution for ThreadAbortException

Posted by Vakul Kumar More on April 13, 2011


From the early days of using ASP.NET, we use Server.Transfer(URL) and Response.Redirect(URL). These simple calls used in ASP would never create problems but, in ASP.NET an exception is thrown when used.

The ThreadAbortException is thrown when a call is made to Response.Redirect(url). This happens because the system aborts processing of the current web page thread after it sends the redirect to the response stream. Internally, Response.Redirect(url) makes a call to Response.End() and that call Thread.Abort() which forces the stack to end the thread.

Many suggest using Try and Catch block. I feel it may not be a proper solution as processing exception can be costly. Best option would be to use an overload method of Response.Redirect() to redirect without calling Response.End().

Response.Redirect(string url, bool endResponse);

Response.Redirect(url, true) The client will be sent the redirect for the new page and present page will immediately stop processing as a thread abort will occur.  This is the default behavior of a redirect.

Response.Redirect(url, false) – In this overload method, the second parameter tells the system whether to make the internal call to Response.End() or not. Parameter should be false to make the client is sent to redirect url but call to Response.End is skipped. This is one way to avoid the exception, but the cost is that this thread doesn’t stop executing the Application events!

If you are doing a redirect in Page_Init (or any page events) and call Response.Redirect(url, false) the page will only redirect once the current page is done executing.  This means that any server side processing you are performing on that page WILL get executed.

Though, there are cons of using both methods I personally prefer using Respose.Redirect(url,false).

Advertisements

4 Responses to “Response.Redirect(url) : Solution for ThreadAbortException”

  1. sanwar said

    Excellent solution, Thanks.

  2. Good post however I was wondering if you could write a litte more on
    this subject? I’d be very thankful if you could elaborate a little bit more. Many thanks!

  3. you are actually a just right webmaster. The
    web site loading speed is incredible. It seems that you
    are doing any distinctive trick. Also, The contents are
    masterwork. you have done a great process in this subject!

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

 
%d bloggers like this: