以下摘抄自博问: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报错的更多相关文章

  1. aspx页面使用ajax遇到try catch中使用Response.End()报错

    1.使用Ajax接收数据,在返回Response.Write()后应该调用Response.End()才能将数据写入到调用的页面,才能被jQuery的回调函数获取到返回的JSON数据 2.在try-- ...

  2. 修改response,报错Cannot call getWriter(), getOutputStream() already called

    往response里面改数据,然后系统报这个错 此时直接return null即可解决 但是,要想返回相应的页面呢? 可以直接在response里设置返回的页面

  3. Java Web报错:getOutputStream() has already been called for this response解决方案

    今天做了个导出excel表的功能.大概代码如下: ouputStream = response.getOutputStream(); wb.write(ouputStream); ouputStrea ...

  4. Webservice报错客户端发现响应内容类型为“application/json;charset=UTF-8”,但应为“text/xml”。

    控制台对接Webservice正常,同样的方法在Web项目上报错: 客户端发现响应内容类型为“application/json;charset=UTF-8”,但应为“text/xml”.请求失败,错误 ...

  5. 报错记录:getOutputStream() has already been called for this response

    仅作记录:参考文章:http://www.blogjava.net/vickzhu/archive/2008/11/03/238337.html 报错信息: java.lang.IllegalStat ...

  6. java导出excel报错:getOutputStream() has already been called for this response

    对于java导出excel报错的问题,查了很多都说是在使用完输出流以后调用以下两行代码即可 out.clear(); out = pageContext.pushBody(); 但这也许是页面上输出时 ...

  7. Error fetching https://gems.ruby-china.org/: bad response Not Found 404 (https://gems.ruby-china.org/specs.4.8.gz) 报错解决办法

    执行换源操作 gem source -a https://gems.ruby-china.org/ 时报错: Error fetching https://gems.ruby-china.org/: ...

  8. tengine2.2.3报错502的The proxy server received an invalid response from an upstream server问题处理

    tengine2.2.3报错502的The proxy server received an invalid response from an upstream server问题处理 现象:访问订单的 ...

  9. Chrome 控制台报错Unchecked runtime.lastError: The message port closed before a response was received

    Chrome浏览器控制台报错提示 Unchecked runtime.lastError: The message port closed before a response was received ...

随机推荐

  1. MyLinkedList

    /** * 节点类 * @author JP * */ class Node { Object value;//节点元素值 Node pre;//上一个节点 Node next;//下一个节点 pub ...

  2. centos7 python3 Saltstack配置

    Python安装完毕后,提示找不到ssl模块 pip is configured with locations that require TLS/SSL, however the ssl module ...

  3. Python Select模型

    IO多路复用 IO多路复用就是我们经常说的select epoll.select和epoll的好处是单个process就可以同时处理多个网络IO.基本原理是select\epoll会不断的轮询所负责的 ...

  4. JavaScript实用的例子

    ---恢复内容开始--- 1.发送验证码 <input id="send" type="button" value="发送验证码"&g ...

  5. 关于SQL语言的初步认识

    关于SQL语言的初步认识 1.一个SQL数据库是表(Table)的集合,它由一个或多个SQL模式定义. 2.一个SQL表由行集构成,一行是列的序列(集合),每列与行对应一个数据项. 3.一个表或者是一 ...

  6. 01_2_Servlet简介

    01_2_Servlet简介 1. Servlet简介 Servlet是服务器小应用程序 用来完成B/S架构下,客户端请求的响应处理 平台独立,性能优良,能以线程方式运行 Servlet API为Se ...

  7. iOS开发-动画总结

    一.简介 IOS 动画主要是指Core Animation框架.官方使用文档地址为:Core Animation Guide.Core Animation是IOS和OS X平台上负责图形渲染与动画的基 ...

  8. bash编程之case语句,函数

    bash脚本编程:之case语句   条件测试: 0: 成功 1-255: 失败   命令: [ expression ] [[ expression ]] test expression   exP ...

  9. Chunky Monkey-freecodecamp算法题目

    Chunky Monkey(猴子吃香蕉, 分割数组) 要求 把一个数组arr按照指定的数组大小size分割成若干个数组块. 思路 利用size值和while语句确定切割数组的次数(定义temp将siz ...

  10. Unity基础-Input接口

    input 底层的设备输入接口,在开发中很少用到 Input.GetKey() // Update is called once per frame void Update () { if (Inpu ...