19

I'm getting the following exception:

System.Threading.ThreadAbortException: Thread was being aborted.
at System.Threading.Thread.AbortInternal() at System.Threading.Thread.Abort(Object stateInfo) at System.Web.HttpResponse.End() at System.Web.HttpResponse.Redirect(String url, Boolean endResponse)
at System.Web.HttpResponse.Redirect(String url) at taxi_selection.lnkbtnconfirm_Click(Object sender, EventArgs e)

I found that the solution for this is to use:

Response.Redirect("home.aspx",false);

but again this error is occurring.

What is a good solution for this?

my code snippets :

try
{
    Decimal Amount = 0;
    Int64 CabId = 0;
    String CabName = "";
    String CarImage = "";

    foreach (DataListItem gr in dtlstcars.Items)
    {
        RadioButton objcheck = (RadioButton)gr.FindControl("rdbtncarchecked");
        if (objcheck.Checked == true)
        {
            Literal ltrid = new Literal();
            ltrid = (Literal)gr.FindControl("ltrid");

            Label lbtaxiname = (Label)gr.FindControl("lbtaxiname");
            Label lbonewaycarprice = (Label)gr.FindControl("lbonewaycarprice");
            Label lbtwowaycarprice = (Label)gr.FindControl("lbtwowaycarprice");
            Image imgcar = (Image)gr.FindControl("imgcar");

            if (ltrid != null && lbtaxiname != null && imgcar != null && lbonewaycarprice != null && lbtwowaycarprice != null)
            {
                if (lbrootype.Text == "One")
                {
                    Amount = Convert.ToDecimal(lbonewaycarprice.Text);
                }
                else
                {
                    Amount = Convert.ToDecimal(lbtwowaycarprice.Text);
                }
            }
            CabId = Convert.ToInt64(ltrid.Text);
            CabName = lbtaxiname.Text;
            CarImage = imgcar.ImageUrl;
        }
   }
   if (lbroottype.Text != String.Empty && lbrouteid.Text != String.Empty && lbfrom.Text != String.Empty && lbpickupdate.Text != String.Empty && lbto.Text != String.Empty && lbpickupdate.Text != String.Empty && lbpickuptime.Text != String.Empty)
   { 
        Session.Add("BookingDetail", BookingDetail(lbroottype.Text, Convert.ToInt64(lbrouteid.Text), lbfrom.Text, lbto.Text, Convert.ToDateTime(lbpickupdate.Text), lbpickuptime.Text, Convert.ToDateTime(lbreturndate.Text), String.Empty, CabId, CabName, CarImage, Amount, txtPickupaddress.Text, txtDropaddress.Text, txtlandmark.Text, txtname.Text, ddmobilestdcode.SelectedValue, txtmobileno.Text, ddalternatestdcode.SelectedValue, txtalternateno.Text, txtemail.Text, lbdays.Text));//3
       Session.Remove("cart");
       Session.Remove("editcart");
       Response.Redirect("confirm");
   }
   else
   {
       Response.Redirect("home");
   }
}
catch (Exception ext)
{
    String msg = ext.Message;
    da.InsertRecordWithQuery("insert error_tbl values('" + msg + "')");
}
Koby Douek
  • 16,156
  • 19
  • 74
  • 103
Kavita
  • 225
  • 1
  • 3
  • 12

3 Answers3

33

http://support.microsoft.com/kb/312629

as you can see here the problem is that you are attempting to use response.redirect in a try/catch block. It thrown an exception.

Your solution of changing the call to be Response.Redirect(url, false) should work. You need to make sure to do it on every Response.Redirect call.

Also note that this will continue execution, so you will have to handle that (prevent it from continuing in some other way).

AnthonyM
  • 1,115
  • 2
  • 10
  • 20
14

This is the way the Redirect works when you do not let the rest of the page continue to run. Its stop the thread and throw that abort exception. You can simple ignore it as:

try
{
    Response.Redirect("newpage.aspx", true);
}
catch (System.Threading.ThreadAbortException)
{
    // ignore it
}
catch (Exception x)
{

}

Attention

If you call the redirect with out stopping the rest of the processing, a hack that can stop the redirect process using a plugin like the NoRedirect can see your rest of the page .!

To prove my point here I make a question about : Redirect to a page with endResponse to true VS CompleteRequest and security thread

Community
  • 1
  • 1
Aristos
  • 66,005
  • 16
  • 114
  • 150
  • Don't do this. Technically, you can catch `ThreadAbortException` here, but it will continue to bubble. Calling `Response.End()` is [generally felt as being bad](http://stackoverflow.com/questions/1087777/is-response-end-considered-harmful/3917180#3917180) because of the harshness of it. In most cases, you can get away with using `HttpApplication.CompleteRequest()` instead. – Sumo Feb 01 '13 at 06:07
  • @Sumo If you not stop the compile of the page then any hacker can see the rest of the render. Yes you catch and ignore the thread abort, I do not understand what is the issue, this is what you do - you abort the thread to move to other page. – Aristos Feb 01 '13 at 06:14
  • It is never good to raise [ThreadAbortExceptions](http://msdn.microsoft.com/en-us/library/system.threading.threadabortexception(v=vs.100).aspx). They are a special case and will be raised again at the end of the catch block. The finally blocks will run before it attempts to destroy the thread and there is no guarantee that it will do so. There are alternatives to prevent finishing the render. I updated my answer to the link that demonstrates it. – Sumo Feb 01 '13 at 06:20
  • Compare your point to overriding Render and RaisePostBackEvent, as I say in my answer to the solution of avoiding rendering anything. This is better than aborting threads. I've also given an answer in your new question. – Sumo Feb 01 '13 at 07:00
4

Response.Redirect without specifying the endResponse parameter as false (default is true) will call Response.End() internally and therefore will trigger a ThreadAbortException to stop execution.

One of two things are recommended here:

  1. If you need to end the response, do not do it in a try/catch. This will cause the redirect to fail.

  2. If you do not need to end the response, call this instead:

    Response.Redirect(url, false);

Within try/catch:

try {
    // do something that can throw an exception
    Response.Redirect(url, false);
    HttpContext.Current.ApplicationInstance.CompleteRequest();
} catch (SomeSpecificException ex) {
    // Do something with the caught exception
}

To avoid postback handling and HTML rendering, you need to do more:

http://web.archive.org/web/20101224113858/http://www.c6software.com/codesolutions/dotnet/threadabortexception.aspx

Sumo
  • 4,066
  • 23
  • 40
  • I will make a question and answer with your code and mine and I describe there my point.... – Aristos Feb 01 '13 at 06:31
  • Ok, read: http://stackoverflow.com/questions/14641143/redirect-to-a-page-with-endresponse-to-true-vs-completerequest-and-security-thre – Aristos Feb 01 '13 at 06:50