27

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.

dhilt
  • 18,707
  • 8
  • 70
  • 85
Sai Avinash
  • 4,683
  • 17
  • 58
  • 96
  • 1
    possible duplicate of [Why Response.Redirect causes System.Threading.ThreadAbortException?](http://stackoverflow.com/questions/2777105/why-response-redirect-causes-system-threading-threadabortexception) – Ryan Gates Oct 30 '13 at 18:03

5 Answers5

39

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
Misam
  • 4,320
  • 2
  • 25
  • 43
Moiz Tankiwala
  • 6,070
  • 7
  • 38
  • 51
  • 4
    Context.ApplicationInstance.CompleteRequest(); – Matt Jan 29 '15 at 14:59
  • 1
    This is the better answer as it avoids the exception in the first place. I wish I would have known about this a few years ago! – Michael Silver Mar 29 '16 at 20:14
  • Make sure Context is capitalized though as it is the Page.Context object, the edit by @Aheho made, while in good faith, was incorrect. – kaelle Feb 16 '18 at 23:51
25

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.

Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
  • 1
    So,Using Response.redirect("Edititem.aspx",false) avoids the occurance of Thread Abort Exception – Sai Avinash Aug 27 '13 at 13:12
  • 2
    @Avinash, it does avoid the exception, but it also doesn't close the response, so the redirect can't happen until you call `End` yourself. When calling `End`, the exception will still be thrown. – Mike Perrenoud Aug 27 '13 at 13:14
  • 4
    If you were to pass `false` into the second overload it would not call `End` automatically. However, the redirect that you requested could not occur until you called `End` on that response, so when you call `End` on your own you will still get the exception. It's not the `Redirect` method that throws the exception, it's the `End` method. – Mike Perrenoud Aug 27 '13 at 15:36
3

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

Robert Columbia
  • 6,313
  • 15
  • 32
  • 40
0

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

  • 4
    Welcome to SO! While this is an answer, it could be more useful if it had more detail about why that would work, so your post may do better if you add some more information. – Mike Precup Aug 02 '14 at 06:48
0
Response.Redirect("Location", false);

It works fine without "ThreadAbortException".

ouflak
  • 2,458
  • 10
  • 44
  • 49