我们使用时线程正在中止

System.Threading.ThreadAbortException:线程正在中止。
System.Web.HttpResponse.Redirect(String url,Boolean endResponse)上的System.Web.HttpResponse.End()处的System.Threading.Thread.AbortInternal()处于System.Threading.Thread.Abort(Object stateInfo)处的System.Threading.Thread.AbortInternal()
在taxi_Web.HttpResponse.Redirect(String url)at taxi_selection.lnkbtnconfirm_Click(Object sender,EventArgs e)

我发现这个解决方案是

的Response.Redirect( “home.aspx”,假); 但是这个错误再次发生。

对此有什么好的解决方案?

我的代码片段:

try { Decimal Amount = 0; Int64 CabId = 0; String CabName = ""; String CarImage = ""; foreach (DataListItem gr in dtlstcars.Items) { //if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) //{ 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 + "')"); } 

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

正如您在此处看到的,问题是您尝试在try / catch块中使用response.redirect。 它引发了一个例外。

您将调用更改为Response.Redirect(url, false)解决方案应该有效。 您需要确保在每个Response.Redirect调用上执行此操作。

另请注意,这将继续执行,因此您必须处理(阻止它以其他方式继续)。

当您不让页面的其余部分继续运行时,这就是Redirect的工作方式。 它停止线程并抛出中止exception。 你可以简单地忽略它:

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

注意

如果您在没有停止剩余处理的情况下调用重定向,那么可以使用NoRedirect等插件停止重定向过程的黑客可以看到您的其余部分。

为了certificate我的观点,我提出了一个问题: 重定向到一个页面,其中endResponse为true VS CompleteRequest和安全线程

Response.Redirect没有将endResponse参数指定为false (默认为true )将在内部调用Response.End() ,因此将触发ThreadAbortException以停止执行。

这里推荐两件事之一:

  1. 如果您需要结束响应,请不要在try / catch中执行此操作。 这将导致重定向失败。

  2. 如果您不需要结束响应,请改为:

    Response.Redirect(url,false);

在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 } 

要避免回发处理和HTML呈现,您需要执行更多操作:

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