2

I have found numerous articles and blog posts which go into detail of the various ways this can be accomplished. Some look involved with custom coding, and are (right at this moment) not what I am wanting (I want a configuration solution). Some of these posts (SO included) share how to do this through configuration over custom coding, but to no success on my end. Here is my attempt so far. Have I overlooked anything? Is my Web.config to blame, or is there something else I must specify on my request?

Additional Details

  • I am running IIS Express 8 (product version: 8.5.9748.0) on my localhost
  • Windows 8.1
  • I am making requests with Fiddler - but the same response occurs with ajax calls (AngularJS)

Fiddler Get Headers

User-Agent: Fiddler
Host: localhost:5293
Accept: application/json; charset=utf-8, application/json Accept-Encoding: gzip

JS Request (AngularJS)

$http.get('localhost:5293/api/Whatever', {
    headers: {
        'Accept': 'application/json; charset=utf-8, application/json',
        'Accept-Encoding': 'gzip'
    }
});

Problem

My response is always

application/json; charset=utf-8

Efforts

  • I have enabled Dynamic Content Compression and Static Content Compression on Windows features under IIS (Turn Windows features on or off)
  • Reset Windows Process Activation Service
  • Run following commands, and verified the results as such

    appcmd.exe set config -section:system.webServer/httpCompression /+"dynamicTypes.[mimeType='application/json',enabled='True']" /commit:apphost
    
    appcmd.exe set config -section:system.webServer/httpCompression /+"dynamicTypes.[mimeType='application/json; charset=utf-8',enabled='True']" /commit:apphost
    

Verify Command:

appcmd.exe list config -section:system.webServer/httpCompression

Verify Response:

<system.webServer>
  <httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files">
    <staticTypes>
      <add mimeType="text/*" enabled="true" />
      <add mimeType="message/*" enabled="true" />
      <add mimeType="application/javascript" enabled="true" />
      <add mimeType="application/atom+xml" enabled="true" />
      <add mimeType="application/xaml+xml" enabled="true" />
      <add mimeType="*/*" enabled="false" />
    </staticTypes>
    <dynamicTypes>
      <add mimeType="text/*" enabled="true" />
      <add mimeType="message/*" enabled="true" />
      <add mimeType="application/x-javascript" enabled="true" />
      <add mimeType="application/javascript" enabled="true" />
      <add mimeType="*/*" enabled="false" />
      <add mimeType="application/json" enabled="true" />
      <add mimeType="application/json; charset=utf-8" enabled="true" />
    </dynamicTypes>
    <scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" />
  </httpCompression>
</system.webServer>

I have placed this exact configuration in my Web.config file under the <system.webServer> node, and still no luck...

Web Api Controller Method

public JsonResult Get()
{
    return new JsonResult
    {
        Data = "foo",
        ContentType = "application/json; charset=utf-8",
        JsonRequestBehavior = JsonRequestBehavior.AllowGet
    };
}
scniro
  • 16,844
  • 8
  • 62
  • 106

1 Answers1

0

Anyway, don't dictate the format. Let content negotiation do its job. And compression will start working too:

public IHttpActionResult Get()
{
    return Ok("foo")
}
abatishchev
  • 98,240
  • 88
  • 296
  • 433
  • Hey thank you for your answer. I just tried this and composed a GET fiddler request. Still no compression and same content-type: application/json; charset=utf-8 – scniro Mar 14 '15 at 20:47
  • @salniro: You may need to use [this](https://github.com/WebApiContrib/WebAPIContrib/blob/master/src/WebApiContrib/Content/CompressedContent.cs). Also see [that](https://github.com/azzlack/Microsoft.AspNet.WebApi.MessageHandlers.Compression). – abatishchev Mar 14 '15 at 20:49
  • Thanks for the resources. I too came across the `CompressedContent` method. I rather not though, at this moment. Is there not a straightforward way to configure this? – scniro Mar 14 '15 at 20:58