C# Thread was being aborted

先重现问题

1、新建一个aspx页面项目,插入两个页面WebForm1.aspx,WebForm2.aspx,

WebForm1代码修改如下

protected void Page_Load(object sender, EventArgs e)
{
    try
    {
        Response.Redirect("WebForm2.aspx");
    }
    catch (Exception exception)
    {
        Console.WriteLine(exception);
        throw;
    }
}

2、在浏览器中打开WebForm1.aspx,页面正常跳转到了WebForm2.aspx,但是如果在以下代码加上断点的话,会有Thread was being aborted异常抛出

 

 3、问题原因
response.redirect();

response.write();

response.end();

会导致当前进程Abort,线程中止,Abort导致异常,虽然这个问题对实际代码运行没有影响,但是如果使用日志系统的话,会导致日志文件中有大量的Error错误。

4、解决办法
修改代码如下

 

 或者

 

posted @ 2019-10-23 09:42  zhaogaojian  阅读(1522)  评论(0编辑  收藏  举报