Response.Redirect throws an “Thread was being aborted”

In Response.Redirect() is used to redirect from current url to a different URL. It does by sending 302 response and makes another round trip redirecting to a new page. This always generate “Thread was being aborted exception”

Example of Resposne.Redirect() Method

The above is a typical way of redirecting to the new page in ASP.NET programming. But here is something the code does when you see the logs/exceptions. while doing Response.Redirect() the .net will automatically throw an System.Threading.ThreadAbortException when the redirect is called. This is a default behavior of .net if you do not pass any boolean value or if you pass “true” in the method.

Resolving the error thread was being aborted.

There are multiple ways to avoid the thread aborted exception and here are the best ways to do it.

Solution 1

Set the EndResponse to false in the Response.Redirect() method. By passing false you are not going to end a response and it help causing the thread abort exception. Below is the example of implementing the same.

Solution 2

Handle the Exception using try catch statement as shown below.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

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