title: Compress JavaScript and CSS in Asp.Net WebForm
body: <p>I use C# Asp.net 4,
  I have this script in Global.asax. I'm able to gzip all asp.net pages but NOT JavaScripts and CSS.
  I would like to know if is possible have compressed CSS and JS directly in my web app or it possible just in IIS. If is possible please point me out what I miss in my code.
  Thanks</p>
  
  <pre><code>protected void Application_PreRequestHandlerExecute(object sender, EventArgs e)
      {
          HttpApplication app = sender as HttpApplication;
          string acceptEncoding = app.Request.Headers["Accept-Encoding"];
          Stream prevUncompressedStream = app.Response.Filter;
  
          if (!(app.Context.CurrentHandler is Page ||
              app.Context.CurrentHandler.GetType().Name == "SyncSessionlessHandler") ||
              app.Request["HTTP_X_MICROSOFTAJAX"] != null)
              return;
  
          if (acceptEncoding == null || acceptEncoding.Length == 0)
              return;
  
          acceptEncoding = acceptEncoding.ToLower();
  
          // if (acceptEncoding.Contains("deflate") || acceptEncoding == "*")
          if (acceptEncoding.Contains("gzip") || acceptEncoding == "*")
          {
              // gzip
              app.Response.Filter = new GZipStream(prevUncompressedStream,
                  CompressionMode.Compress);
              app.Response.AppendHeader("Content-Encoding", "gzip");
  
          }
          // else if (acceptEncoding.Contains("gzip"))
          else if (acceptEncoding.Contains("deflate"))
          {
              // defalte
              app.Response.Filter = new DeflateStream(prevUncompressedStream,
              CompressionMode.Compress);
              app.Response.AppendHeader("Content-Encoding", "deflate");
          }
      }
  </code></pre>