Cleanup and Compression Filters (ASP.NET MVC)

We have pretty common code that 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'm trying to use Response.Flush() to deliver part of the page to improve the user interface. In this case, when response.Filter changed by each write operation, it is clear that the page should be delivered immediately. How can I make my application write in the intermediate stream, then compress it, and then click on response.Filter ?

+4
source share
2 answers

So far this is apparently not solvable because asp.net uses the same stream for input and output for filters

+1
source

I don't think this is doable, but if you need to improve performance and user experience, you can do the following:

1- Use IIS compression, no need to reinvent the wheel 2- Use output caching for actions that its contents will not change frequently. 3- Use Partial Rendering, you first output the most important parts on your page, and then issue Ajax requests to load the rest of the page content, so you can deliver the page to pieces

-2
source

All Articles