冲洗和压缩过滤器 (ASP.NET MVC)

发布于 2024-08-15 20:49:49 字数 1249 浏览 5 评论 0原文

我们有非常通用的代码,运行良好:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寒冷纷飞旳雪 2024-08-22 20:49:49

到目前为止,这似乎无法解决,因为 asp.net 使用相同的流作为过滤器的输入和输出

So far, this seems to be not solvable, because asp.net use same stream for input and output for filters

糖果控 2024-08-22 20:49:49

我认为这是不可行的,但如果您需要提高性能和用户体验,那么您可以执行以下操作:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文