title: Enable HTTP compression with ASP.NET Web API
body: <p>We serve files for a website from our Asp .NET Web API:</p>
  
  <pre><code>public class Startup
  {
      public void Configuration(IAppBuilder app)
      {
          var clientHostname = System.Configuration.ConfigurationManager.AppSettings["ClientHostname"];
  
          var staticFileOptions = new StaticFileOptions()
          {
              OnPrepareResponse = staticFileResponseContext =&gt;
              {
                  staticFileResponseContext.OwinContext.Response.Headers.Add("Cache-Control", new[] { "public", "max-age=0" });
              }
          };
  
          app.MapWhen(ctx =&gt; ctx.Request.Headers.Get("Host").Equals(clientHostname), app2 =&gt;
          {
              app2.Use((context, next) =&gt;
              {
                  if (context.Request.Path.HasValue == false || context.Request.Path.ToString() == "/") // Serve index.html by default at root
                  {
                      context.Request.Path = new PathString("/Client/index.html");
                  }
                  else // Serve file
                  {
                      context.Request.Path = new PathString($"/Client{context.Request.Path}");
                  }
  
                  return next();
              });
  
              app2.UseStaticFiles(staticFileOptions);
          });
      }
  }
  </code></pre>
  
  <p>I want to enable HTTP compression. According to <a href="https://docs.microsoft.com/en-us/aspnet/core/performance/response-compression?tabs=aspnetcore2x" rel="nofollow noreferrer">this MSDN documentation</a></p>
  
  <blockquote>
    <p>Use server-based response compression technologies in IIS, Apache, or Nginx where the performance of the middleware probably won't match that of the server modules. Use Response Compression Middleware when you're unable to use:</p>
    
    <ul>
    <li><p>IIS Dynamic Compression module</p></li>
    <li><p>Apache mod_deflate module</p></li>
    <li><p>NGINX Compression and Decompression</p></li>
    <li><p>HTTP.sys server (formerly called WebListener)</p></li>
    <li><p>Kestrel</p></li>
    </ul>
  </blockquote>
  
  <p>So I think the first preferable way to do this in my instance is with IIS Dynamic Compression Module. Accordingly, I tried this in my Web.config, as a test, following <a href="https://docs.microsoft.com/en-us/iis/configuration/system.webserver/httpcompression/dynamictypes/#configuration-sample" rel="nofollow noreferrer">this example</a>:</p>
  
  <pre><code>&lt;configuration&gt;
    &lt;system.webServer&gt;
      &lt;httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files"&gt;
        &lt;dynamicTypes&gt;
          &lt;add mimeType="*/*" enabled="true" /&gt;
        &lt;/dynamicTypes&gt;
        &lt;staticTypes&gt;
          &lt;add mimeType="*/*" enabled="true" /&gt;
        &lt;/staticTypes&gt;
      &lt;/httpCompression&gt;
    &lt;/system.webServer&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>However, the response headers do not include <code>Content-Encoding</code>, so I don't believe it is being compressed. What am I missing? How can I set this up to serve with compression in the best way possible?</p>
  
  <p>I have verified that my client is sending an <code>Accept-Encoding</code> header of <code>gzip, deflate, br</code>.</p>
  
  <p>Update</p>
  
  <p>I tried installing Dynamic HTTP Compression in IIS as it is not installed by default. It seems to me I am trying to serve content statically, but I thought this was worth a try. I verified that both static and dynamic content compression are enabled in IIS Manager. However, I re-ran it but still no compression.</p>
  
  <p>Update 2</p>
  
  <p>I realized compression was working on our Azure servers but still not with my local IIS.</p>