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.
At first.
IIS Ignores WEB API Response compression because web api responses are of mime type
Default IIS Compression settings do not include this Mime type so they do not compress the response.
So your have to add this mime type to the
<dynamicTypes>
section<add mimeType="application/json; charset=utf-8" enabled="true" />
(Or just to test it , as you did
<add mimeType="*/*" enabled="true" />
)BUT
By default
<httpCompression>
Section is locked by IIS for settings from outside!!So any settings you specify on your web.config will get ignored!
Unless you specify these settings on the applicationHost.config
<httpCompression>
section OR edit the<httpCompression>
section to allow settingsfrom outside.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;
2) Inside WebApiConfig.cs add these usings;
3) Inside WebApiConfig.cs add to the bottom of Register(HttpConfiguration config);
4) Edit your web.config and inside system.webServer add;
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...
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.
According to ASP.NET Web API Compression (Ben Foster Blog) you have two options:
Change your
applicationHost.config
and addto httpCompression -> dynamicTypes section.
Use an delegating handler in your web api pipeline to handle the compression.
e.g. Fabrik.Common or Microsoft ASP.NET Web API Compression Support
In your implementation of the NuGet package Microsoft.AspNet.WebApi.MessageHandlers.Compression, did you add the requisite line (below) to your App_Start\WebApiConfig.cs file? Note that is must be after all other message handlers in that same method, as per instructions on the package's home site.
Here's a sample Register method from a WebApiConfig file that I used in a blog:
This is the blog post I mentioned: .Net Web API Compression
I think you have made all the homework in the server side, but the problem is in the request.
To allow the server to compress the response it's neccessary to inclue this header in the request:
If you don't do it, no matter what you do on the server side, the response will never be compressed.
You don't specify which is your Web API client, but there is always a way to add headers in the request using the client API.