Response.End报错

以下摘抄自博问:https://q.cnblogs.com/q/31506/       try catch中使用Response.End()

我在WebForm中用ajax发送请求到页面index.aspx,然后我在index.aspx.cs做数据处理,当处理好之后,我用Response.Write()输出我想要的数据,我在index.aspx.cs所作的数据处理是放在一个try---catch中的(因为有访问到数据库),当我输出我想要的数据后,我了Response.End()终止当前页面的运行,因为我只想要我自己输出的数据,而不想要连页面的html代码也输出来,但是会报错,我不明白为什么,难道在try--catch里面不能用Response.End()吗,如果不能弄,我该如何终止向客户端输出html代码?

我有想过用个一般处理程序,但是我现在的需求是不能用一般处理程序的,请教一下各位大牛,这种情况该怎么办?

调用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/Tpf386/p/7119772.html