我们使用时线程正在中止

Kav*_*ita 14 c# asp.net

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

我发现解决方案就是这样

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

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

我的代码片段:

Response.Redirect("home.aspx",false);
Run Code Online (Sandbox Code Playgroud)

Ant*_*nyM 28

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

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

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

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


Ari*_*tos 12

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

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

}
Run Code Online (Sandbox Code Playgroud)

注意

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

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


Sum*_*umo 6

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

这里推荐以下两件事之一:

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

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

    Response.Redirect(url, false);

在尝试/捕获中:

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
}
Run Code Online (Sandbox Code Playgroud)

为了避免回发处理和 HTML 渲染,您需要执行更多操作:

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