【问题标题】:Differences between Response.End() and Response.Flush()Response.End() 和 Response.Flush() 的区别
【发布时间】:2012-01-19 11:33:16
【问题描述】:

我有这样的代码:

context.HttpContext.Response.Clear();
context.HttpContext.Response.Write(htmlString);              
context.HttpContext.Response.End(); 

但是当页面加载时,我在其中有一个未关闭的 HTML 标记。当我将 Response.End() 替换为 Response.Flush() 时,它工作正常。

Response.End()Response.Flush() 有什么区别?

【问题讨论】:

    标签: asp.net httpresponse


    【解决方案1】:

    Response.Flush

    强制将所有当前缓冲的输出发送到客户端。这 请求处理过程中可以多次调用flush方法。

    响应。结束

    将所有当前缓冲的输出发送到客户端,停止执行 页面,并引发 EndRequest 事件。

    如果您在 Response.Write 之后没有对页面进行任何处理并且想要停止处理该页面,则应该尝试使用此代码。

        context.HttpContext.Response.Clear();
        context.HttpContext.Response.Write(htmlString);              
        context.HttpContext.Response.Flush(); // send all buffered output to client 
        context.HttpContext.Response.End(); // response.end would work fine now.
    

    【讨论】:

    • 我很好奇这里是否真的需要在End() 之前调用Flush()?根据您提供的定义,End 在停止页面执行并引发EndRequest 之前与Flush 执行相同的操作...那么为什么在End() 之前调用Flush() 是谨慎的?
    • 没关系,我的代码中有一个涉及上述代码的复杂设置,当我删除 Flush() 时,我收到异常,指出线程正在中止。
    • 从显示的文档看来,您似乎不需要在 End 之前调用 Flush,但实际上在没有 Flush 的情况下调用 End 时会发生各种错误。
    • 有趣,“但实际上在调用 End without Flush 时会发生各种错误”,我遇到了一个问题“System.Web.HttpException:发送 HTTP 标头后服务器无法设置状态。 "当我立即调用 Flush 后是 End 所以我正在尝试注释掉 Flush 行。
    猜你喜欢
    • 2011-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-23
    • 1970-01-01
    • 2010-12-06
    • 1970-01-01
    • 2018-02-26
    相关资源
    最近更新 更多