asp.net - "Invalid use of response filter"压缩来自 IHt

我有一个返回文件的 IHttpHandler。压缩响应流时,自动使用 Telerik RadCompression 或使用

显式设置过滤器
context.Response.Filter = new GZipStream(context.Response.Filter, CompressionMode.Compress);

返回给浏览器的响应是正确的,但响应的末尾是一些 HTML。 HTML 包含异常:

[HttpException (0x80004005): Invalid use of response filter]
   System.Web.HttpResponseStreamFilterSink.VerifyState() +3928894
   System.Web.HttpResponseStreamFilterSink.Write(Byte[] buffer, Int32 offset, Int32 count) +28
   System.IO.Compression.DeflateStream.Dispose(Boolean disposing) +363
   System.IO.Stream.Close() +28
   System.IO.Compression.GZipStream.Dispose(Boolean disposing) +63
   System.IO.Stream.Close() +28
   System.IO.Compression.DeflateStream.Dispose(Boolean disposing) +595
   System.IO.Stream.Close() +28
   System.IO.Compression.GZipStream.Dispose(Boolean disposing) +63
   System.IO.Stream.Close() +28
   System.Web.HttpWriter.FilterIntegrated(Boolean finalFiltering, IIS7WorkerRequest wr) +754
   System.Web.HttpResponse.FilterOutput() +121
   System.Web.CallFilterExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +174

如果我确定响应未压缩,则响应不包含异常。

这是怎么回事?

最佳答案

确保您没有阻止缓冲。显然,要压缩响应,您必须允许对其进行缓冲。

即,需要删除以下两行之一:

context.Response.BufferOutput = false;
context.Response.Filter = new GZipStream(context.Response.Filter, CompressionMode.Compress);

关于asp.net - "Invalid use of response filter"压缩来自 IHttpHandler 的响应时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4797829/

相关文章:

visual-studio-2010 - TFS Team Build MSTest 运行持续时间太

maven - 如何在 Maven 中将依赖类包含到 war 项目的 WEB-INF/classes

java - 为什么当我在另一台计算机上加载我的工作区时 Eclipse 显示错误?

maven-3 - mvn clean install + java.lang.NoClassDef

c# - 如何在 C# 中从 IntPtr 获取 byte[]

c# - 从 Datagrid 插入和更新 SQL Server

r - 如何使用 RODBC 或 RCurl 从 R 中受密码保护的 Sharepoint 2007

php - soap 信封是如何生成的,为什么 .NET 和 php 客户端会生成/接收不同的 so

unicode - 无法使用自定义操作将 MSI 安装到用户名中包含非 ascii 字符的非管理员

google-maps - 谷歌地图 API 最大坐标长度?