Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top