我们有非常通用的代码,运行良好:
public class CompressionFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
HttpRequestBase request = filterContext.HttpContext.Request;
if (request.IsAjaxRequest())
return;
string acceptEncoding = request.Headers["Accept-Encoding"];
if (string.IsNullOrEmpty(acceptEncoding)) return;
acceptEncoding = acceptEncoding.ToUpperInvariant();
HttpResponseBase response = filterContext.HttpContext.Response;
if (acceptEncoding.Contains("GZIP"))
{
response.AppendHeader("Content-encoding", "gzip");
response.Filter = new WhitespaceFilter(new GZipStream(response.Filter, CompressionMode.Compress));
}
else if (acceptEncoding.Contains("DEFLATE"))
{
response.AppendHeader("Content-encoding", "deflate");
response.Filter = new WhitespaceFilter(new DeflateStream(response.Filter, CompressionMode.Compress));
}
}
}
现在我正在尝试用它Response.Flush()
来传递部分页面,以改善用户体验。在这种情况下,response.Filter
每次写入操作修改时,很明显需要立即交付页面。如何让我的应用程序写入中间流,然后压缩它,然后推送到Response.Filter
?