Sharma Raushan

Sharma Raushan

  • NA
  • 527
  • 13.7k

Gzip compression with code in asp.net mvc.

Aug 9 2020 8:32 AM
I have shared plesk hosting and have very less access priveleges to IIS configuration , so to enable Gzip compression i followed one of the post discussed here,
https://www.c-sharpcorner.com/Forums/gzip-compression-issue-on-iis-85
 
and implemented the below action filter
  1. public class CompressAttribute:ActionFilterAttribute  
  2.   {  
  3.        public override void OnResultExecuting(ResultExecutingContext filterContext)  
  4.       {  
  5.           HttpRequestBase request = filterContext.HttpContext.Request;  
  6.   
  7.           string acceptEncoding = request.Headers["Accept-Encoding"];  
  8.   
  9.           if (string.IsNullOrEmpty(acceptEncoding)) return;  
  10.   
  11.           acceptEncoding = acceptEncoding.ToUpperInvariant();  
  12.   
  13.           HttpResponseBase response = filterContext.HttpContext.Response;  
  14.   
  15.           if (acceptEncoding.Contains("GZIP"))  
  16.           {  
  17.               response.AppendHeader("Content-encoding""gzip");  
  18.               response.Filter = new GZipStream(response.Filter, CompressionMode.Compress);  
  19.           }  
  20.           else if (acceptEncoding.Contains("DEFLATE"))  
  21.           {  
  22.               response.AppendHeader("Content-encoding""deflate");  
  23.               response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress);  
  24.           }  
  25.       }  
  26.   } 
 This is very good when we don't have access to IIS, but the problem is it works only for Html.
How to compress CSS and Javascript with this approach?or is there any other approach?
 

Answers (7)