Response.End()和Response.Flush()之间的差异

Cip*_*ipi 21 asp.net httpresponse

我有这样的代码:

context.HttpContext.Response.Clear();
            context.HttpContext.Response.Write(htmlString);              
            context.HttpContext.Response.End(); 
Run Code Online (Sandbox Code Playgroud)

但是当页面加载时,我有未公开的html标签.当我用Response.Flush()替换Response.End()时,它工作正常.Response.End()和Response.Flush()有什么区别?

Dot*_*ser 27

Response.Flush

强制将所有当前缓冲的输出发送到客户端.在请求处理期间可以多次调用Flush方法.

到Response.End

将所有当前缓冲的输出发送到客户端,停止执行页面,并引发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.
Run Code Online (Sandbox Code Playgroud)

  • 没关系,我的代码中有一个复杂的设置,涉及上面的代码,当我删除`Flush()时,我得到异常,说明线程正在被中止. (5认同)
  • 我很好奇这里是否真的有必要在“End()”之前调用“Flush()”?根据您提供的定义,`End` 在停止页面执行并引发 `EndRequest` 之前与 `Flush` 做同样的事情......那么为什么在 `End()` 之前调用 `Flush()` 是明智的? (2认同)
  • 从显示的文档中看起来好像你不需要在End之前调用Flush,但实际上在调用End而不用Flush时会发生各种错误. (2认同)