在调用Response.End()时,会执行Thread.CurrentThread.Abort()操作

在调用Response.End()时,会执行Thread.CurrentThread.Abort()操作。

如果将Response.End()放在try...catch中,catch会捕捉Thread.CurrentThread.Abort()产生的异常System.Threading.ThreadAbortException。

解决方法(任选一个):

1. 在catch中排除ThreadAbortException异常,示例代码如下:

复制代码
try
{
    Response.End();
}
catch (System.Threading.ThreadAbortException)
{
}
catch (Exception ex)
{
    Response.Write(ex);
}
复制代码

2. 用Context.ApplicationInstance.CompleteRequest()结束当前请求,代码如下[经测试这个不可以用]:

复制代码
protected void Page_Load(object sender, EventArgs e)
{
    try
    {
        Response.Write("Hello world!");
        this.Page.Visible = false;
        Context.ApplicationInstance.CompleteRequest();
    }
    catch (Exception ex)
    {
        Response.Write(ex);
    }
}
原文地址:https://www.cnblogs.com/tianboblog/p/5276669.html