asp.net mvc compress stream and remove whitespace

前端 未结 2 1187
独厮守ぢ
独厮守ぢ 2021-02-01 10:15

So I am compressing my output stream via an action filter:

var response = filterContext.HttpContext.Response;
response.Filter = new DeflateStream(response.Filter         


        
2条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-01 11:03

    For those who get this far... you can do it... just swap the order of the stream chaining:

       public override void OnActionExecuting(ActionExecutingContext filterContext)
       {
            var response = filterContext.HttpContext.Response;
    
            // - COMPRESS
            HttpRequestBase request = filterContext.HttpContext.Request;
            string acceptEncoding = request.Headers["Accept-Encoding"];
            if (!String.IsNullOrEmpty(acceptEncoding))
            {
                acceptEncoding = acceptEncoding.ToUpperInvariant();
    
                if (acceptEncoding.Contains("GZIP"))
                {
                    response.AppendHeader("Content-encoding", "gzip");
                    response.Filter = new GZipStream(response.Filter, CompressionMode.Compress);
                }
                else if (acceptEncoding.Contains("DEFLATE"))
                {
                    response.AppendHeader("Content-encoding", "deflate");
                    response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress);
                }
            }
    
            // - REMOVE WHITE SPACE
            response.Filter = new WhitespaceFilter(response.Filter);
        }
    

提交回复
热议问题