Sai Avinash
Sai Avinash

Reputation: 4753

Getting Thread Abort Exception while using Response.Redirect()

I wrote the following piece of code in a page which is under Update Panel.

protected void myGrid_RowCommand(object sender, GridViewCommandEventArgs e)
{
    try
    {
        if (e.CommandName =="EditNames")
        {
            long lSelectedName = Convert.ToInt64(e.CommandArgument);
            Session["SelectedItem"] = lSelectedName;
            Response.Redirect("EditItem.aspx");
        }
        else if (e.CommandName =="DeleteNames")
        {
            long lSelectedName = Convert.ToInt64(e.CommandArgument);
            ValidName.DeleteItem(lSelectedName);

            ScriptManager.RegisterStartupScript(this, GetType(), "Key", "alert('Name deleted sucessfully')", true);
        }
    }
    catch (System.Threading.ThreadAbortException)
    {

    }
    catch (Exception ex)
    {
        Error handling code...
    }
}

Here, I am getting a Thread Abort Exception while redirecting. However, I resolved it by using an error handler System.Threading.ThreadAbortException.

But I am unsure why that error came while redirecting. Even though I solved this problem, I would like to know is there any mistake in the way I am coding or is there any way to stop the error firing at all.

Give your inputs...

Note that the page is under AJAX UPDATE PANEL.

Upvotes: 29

Views: 58602

Answers (5)

Response.Redirect("Location", false);

It works fine without "ThreadAbortException".

Upvotes: 0

Mahdi Esmailoghli
Mahdi Esmailoghli

Reputation: 59

This is happening because you are redirecting inside of your try/catch block. Don't do this.

Upvotes: 3

Moiz Tankiwala
Moiz Tankiwala

Reputation: 6300

Please read this article - http://blogs.msdn.com/b/tmarq/archive/2009/06/25/correct-use-of-system-web-httpresponse-redirect.aspx

Instead of ending the request, it is a good practice to bypass the request execution pipeline by calling the Context.ApplicationInstance.CompleteRequest().

So your code would look something like this:

Response.Redirect("TargetPage", false);        //write redirect
Context.ApplicationInstance.CompleteRequest(); // end response

Upvotes: 42

user3098514
user3098514

Reputation: 11

It is enough to mention false in the redirect method, like, Response.Redirect("TargetPage", false);

Upvotes: 0

Mike Perrenoud
Mike Perrenoud

Reputation: 67918

Even though, i solved this problem , i would like to know is there any mistake in the way i am coding

No mistake, you've done well.

This error is expected. It's thrown because the server thread is in fact aborted when redirecting. From the MSDN documentation:

If you specify true for the endResponse parameter, this method calls the End method for the original request, which throws a ThreadAbortException exception when it completes.

and the documentation for the overload you're using:

Redirect calls End which throws a ThreadAbortException exception upon completion.

Upvotes: 25

Related Questions