大网数据广告活动
Web服务器与PHP

ThreadAbortException occurs if you use Response.End

· 52CE 我爱测

This article provides resolutions for ThreadAbortException error that occurs if you use Response.End, Response.Redirect, or Server.Transfer.

This article helps you resolve the ThreadAbortException error that occurs if you use Response.End, Response.Redirect, or Server.Transfer.

Original product version:   ASP.NET on .NET Framework 4.6.2 or later, ASP.NET on .NET Framework 3.5 Service Pack 1
Original KB number:   312629

Symptoms

If you use the Response.End, Response.Redirect, or Server.Transfer method, a ThreadAbortException exception occurs. You can use a try-catch statement to catch this exception.

Cause

The Response.End method ends the page execution and shifts the execution to the Application_EndRequest event in the application's event pipeline. The line of code that follows Response.End isn't executed.

This problem occurs in the Response.Redirect and Server.Transfer methods because both methods call Response.End internally.

Resolution

To work around this problem, use one of the following methods:

  • For Response.End , call the HttpContext.Current.ApplicationInstance.CompleteRequest method instead of Response.End to bypass the code execution to the Application_EndRequest event.

  • For Response.Redirect, use an overload, Response.Redirect(String url, bool endResponse) that passes false for the endResponse parameter to suppress the internal call to Response.End. For example:

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

    If you use this workaround, the code that follows Response.Redirect is executed.

  • For Server.Transfer, use the Server.Execute method instead.

Although ASP.NET handles this exception, you can use the try-catch statement to catch this exception. For example:

try
{
    Response.Redirect("nextpage.aspx");
}
catch (Exception ex)
{
    Response.Write (ex.Message);
}

Add a breakpoint on the Response.Write line, and notice that this breakpoint is hit. When you examine the exception, notice that the ThreadAbortException exception occurs.

Source: Microsoft Learn / IT Pro troubleshooting by Microsoft and contributors. Documentation licensed CC BY 4.0. Code samples are covered by the MIT License. Converted from Markdown, with links normalized and embedded media omitted.

资料来源与适用范围

资料来源
Microsoft Learn / IT Pro troubleshooting
适用范围
See source applicability · Source date 12/13/2024
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 4.0 (documentation), MIT (code samples)

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。