You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
1.3 KiB

2 years ago
  1. using System;
  2. using System.IO.Compression;
  3. using System.Web;
  4. namespace EasyBL.WebApi.Helper
  5. {
  6. public class CompressUtils
  7. {
  8. public static void CompressCssAndJavascript(HttpApplication app)
  9. {
  10. var contentType = app.Response.ContentType;
  11. if (contentType == "application/x-javascript" || contentType == "text/javascript" || contentType == "application/javascript")
  12. {
  13. app.Response.Cache.VaryByHeaders["Accept-Encoding"] = true;
  14. Console.WriteLine(contentType);
  15. var acceptEncoding = app.Request.Headers["Accept-Encoding"];
  16. if (acceptEncoding == null || acceptEncoding.Length == 0) return;
  17. acceptEncoding = acceptEncoding.ToLower();
  18. if (acceptEncoding.Contains("gzip"))
  19. {
  20. app.Response.Filter = new GZipStream(app.Response.Filter, CompressionMode.Compress);
  21. app.Response.AppendHeader("Content-Encoding", "gzip");
  22. }
  23. else if (acceptEncoding.Contains("deflate") || acceptEncoding == "*")
  24. {
  25. app.Response.Filter = new DeflateStream(app.Response.Filter, CompressionMode.Compress);
  26. app.Response.AppendHeader("Content-Encoding", "deflate");
  27. }
  28. }
  29. }
  30. }
  31. }