我通常通过刷新和结束来结束向客户端的传输,如下面的代码所示。
Response.Flush();
Response.End();
但是,Response.End()
将缓冲的内容刷新到客户端,这让我震惊。那么按顺序使用两者有什么意义吗?
最佳答案
简而言之,不,但两者之间存在重要区别:
Response.Flush()
对于在所有处理完成之前写入部分响应(即,如果禁用响应缓冲)非常有用。 Flush() 之后的任何代码都将继续执行,并且任何之前的 Response.Write() 都将刷新到客户端。
Response.End()
有点残酷...它不仅会中止当前线程,而且会抛出 ThreadAbort 异常并终止当前请求。除非正确处理异常,否则 Response.End() 之后的任何代码都永远不会出现。
正如您所指出的,End() 无论如何都会刷新您的缓冲内容。在某些情况下,End() 实际上会在内部调用 Flush(),特别是如果上下文不可取消:
public void End()
{
if (this._context.IsInCancellablePeriod)
{
InternalSecurityPermissions.ControlThread.Assert();
Thread.CurrentThread.Abort(new HttpApplication.CancelModuleException(false));
}
else if (!this._flushing)
{
this.Flush();
this._ended = true;
if (this._context.ApplicationInstance != null)
{
this._context.ApplicationInstance.CompleteRequest();
}
}
}
关于c# - 如果我之后直接使用 `Response.Flush()` ,那么 `Response.End()` 有意义吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11894164/