Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web API Gzip not being applied

I have added the web.config entry to enable gzip compression based on this S/O answer Enable IIS7 gzip.

I then checked the Chrome Developer window while loading an ASPX page and saw the header in the response:

Cache-Control:private
Content-Encoding:gzip
Content-Length:3669
Content-Type:text/html; charset=utf-8
Date:Wed, 04 Mar 2015 00:46:05 GMT
Server:Microsoft-IIS/7.5
Vary:Accept-Encoding
X-AspNet-Version:4.0.30319
X-Powered-By:ASP.NET

So that means it's "working", correct? But when looking for that header when making a Web API call, it is not present:

Cache-Control:no-cache
Content-Length:551
Content-Type:application/json; charset=utf-8
Date:Wed, 04 Mar 2015 00:53:05 GMT
Expires:-1
Pragma:no-cache
Server:Microsoft-IIS/7.5
X-AspNet-Version:4.0.30319
X-Powered-By:ASP.NET

I have tried all kinds of different configurations (starting with the one recommended in the linked S/O answer above). Finally, in an act of desperation, I set it to this, which I thought would make it try to compress all requests (everything except */* commented out):

  <httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files">
    <scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll"/>
    <dynamicTypes>
      <add mimeType="*/*" enabled="true"/>
      <!--<add mimeType="text/*" enabled="true"/>
      <add mimeType="message/*" enabled="true"/>
      <add mimeType="application/javascript" enabled="true"/>
      <add mimeType="application/json" enabled="true"/>-->
      <!--<add mimeType="*/*" enabled="false"/> -->
    </dynamicTypes>
    <staticTypes>
      <add mimeType="*/*" enabled="true"/>
      <!--<add mimeType="text/*" enabled="true"/>
      <add mimeType="message/*" enabled="true"/>
      <add mimeType="application/javascript" enabled="true"/>
      <add mimeType="application/json" enabled="true"/>-->
      <!-- add mimeType="*/*" enabled="false"/>-->
    </staticTypes>
  </httpCompression>
  <urlCompression doStaticCompression="true" doDynamicCompression="true"/>

What can be preventing the GZIP from being applied to my Web API methods?

Update

I have since tried both the NuGet Web API Compression package, and editing the applicationHost.config in both IIS Express 8.0 (Visual Studio) and a locally-installed IIS 7.5. All have yielded the same results: requests for other mime types like text/* work, but application/json refuses to be gzipped.

like image 927
FirstDivision Avatar asked Mar 04 '15 01:03

FirstDivision


People also ask

How do you check gzip compression is enabled or not?

Double click on the file and select headers. Under 'Response headers' you are looking for the 'Connection-Encoding' field, it will say gzip if it is enabled.

Do any browsers not support gzip?

All modern browsers can handle a gzip encoded response. In fact, if you look at their requests, they'll have a header that says something along the lines of Accept-Encoding: gzip which is their way of saying to the server that they can handle gzipped responses.


2 Answers

Is the WebAPI behind a Firewall, Web Proxy, Virus Protection Suite? As mentioned in Even Faster Web Sites: Performance Best Practices for Web Developers By Steve Souders This could be stripping out the headers.

like image 129
Paul Avatar answered Oct 13 '22 18:10

Paul


Thanks to the 2 above solutions and other solutions elsewhere I figured a step by step explanation of how to get http compression working with Web API 2.2 might be beneficial as a few packages/namespaces have changed since the above posts.

1) Using nuget package manager console install the following;

Install-Package Microsoft.AspNet.WebApi.MessageHandlers.Compression

2) Inside WebApiConfig.cs add these usings;

using System.Net.Http.Extensions.Compression.Core.Compressors;
using Microsoft.AspNet.WebApi.Extensions.Compression.Server;

3) Inside WebApiConfig.cs add to the bottom of Register(HttpConfiguration config);

GlobalConfiguration.Configuration.MessageHandlers.Insert(0, new ServerCompressionHandler(new GZipCompressor(), new DeflateCompressor()));

4) Edit your web.config and inside system.webServer add;

<urlCompression doStaticCompression="true" doDynamicCompression="true" dynamicCompressionBeforeCache="true" />
<httpCompression>
    <dynamicTypes>
        <clear />
        <add enabled="true" mimeType="text/*" />
        <add enabled="true" mimeType="message/*" />
        <add enabled="true" mimeType="application/x-javascript" />
        <add enabled="true" mimeType="application/javascript" />
        <add enabled="true" mimeType="application/json" />
        <add enabled="false" mimeType="*/*" />
        <add enabled="true" mimeType="application/atom+xml" />
    </dynamicTypes>
    <staticTypes>
        <clear />
        <add enabled="true" mimeType="text/*" />
        <add enabled="true" mimeType="message/*" />
        <add enabled="true" mimeType="application/javascript" />
        <add enabled="true" mimeType="application/atom+xml" />
        <add enabled="true" mimeType="application/xaml+xml" />
        <add enabled="true" mimeType="application/json" />
        <add enabled="false" mimeType="*/*" />
    </staticTypes>
</httpCompression>

Worked first time on both local and an azure website so hopefully it works for you! Plus certainly no need to mess with applicationHost.config...

like image 33
alv Avatar answered Oct 13 '22 20:10

alv