开发者

Flushing and Compression filters (ASP.NET MVC)

开发者 https://www.devze.com 2022-12-15 02:11 出处:网络
We have quite common code which worked fine: public class CompressionFilterAttribute : ActionFilterAttribute

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 ma开发者_运维问答ke my application to write to an intermediate stream, then compress it, and then push to Response.Filter?


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消