冲洗和压缩过滤器 (ASP.NET MVC)
我们有非常通用的代码,运行良好:
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 ?
We have quite common code which worked fine:
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));
}
}
}
Now I am trying to use Response.Flush()
to deliver part of the page, to improve user experience.
With this scenario, when response.Filter
is modified by each write operation it is clear that the page needs to be delivered at once.
How I can make my application to write to an intermediate stream, then compress it, and then push to Response.Filter
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
到目前为止,这似乎无法解决,因为 asp.net 使用相同的流作为过滤器的输入和输出
So far, this seems to be not solvable, because asp.net use same stream for input and output for filters
我认为这是不可行的,但如果您需要提高性能和用户体验,那么您可以执行以下操作:
1-使用IIS压缩,无需重新发明轮子
2- 将输出缓存用于其内容不会经常更改的操作。
3-使用部分渲染,您将首先输出页面中最重要的部分,然后发出Ajax请求来加载页面的其余内容,这样您就可以分块交付页面
I don't think this is doable, but if you need to improve the performance and user experience then you can do the following:
1- Use IIS compression, no need to reinvent the wheel
2- Use Output caching for actions that its content will not change frequently.
3- Use Partial Rendering, you will output the most important parts in your page first, and then issue Ajax requests to load the rest of page contents, this way you can deliver the page in chunks