asp.net mvc gzip Attribute 启用GZip 压缩

Code ... 

/// <summary>
    /// GZip Compress
    /// </summary>
    public class GZipCompressAttribute : ActionFilterAttribute
    { 
        public override void OnResultExecuted(ResultExecutedContext filterContext)
        {
            if (filterContext.Exception != null)
                return;

            var response = filterContext.HttpContext.Response;
             
            if ((response.Filter is GZipStream) || (response.Filter is DeflateStream))
                return;

            var acceptEncoding = filterContext.HttpContext.Request.Headers["Accept-Encoding"];
            if (!string.IsNullOrEmpty(acceptEncoding))
            {
                if (response.Filter != null)
                {
                    acceptEncoding = acceptEncoding.ToLower();
                    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); 
                    }
                }
            } 
        } 
    }