如何在try catch中使用Response.End()

在调用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);
}
}

posted on 2015-11-30 11:31  YAO000  阅读(367)  评论(0)    收藏  举报