码迷,mamicode.com
首页 > 其他好文 > 详细

Response.End报错

时间:2017-07-05 11:49:37      阅读:87      评论:0      收藏:0      [点我收藏+]

标签:for   运行   tar   tps   ajax   world   pre   调用   报错   

 

以下摘抄自博问: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);
}
}

Response.End报错

标签:for   运行   tar   tps   ajax   world   pre   调用   报错   

原文地址:http://www.cnblogs.com/Tpf386/p/7119772.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!