2

I have an MVC 5 application (C#) hosted on Microsoft Azure. The app returns some fairly large JSON documents from the server to the client. Does anyone know how to turn HTTP compression on so that these documents are compressed going to the client? I've Googled this but I couldn't find anything that wasn't at least 3-4 years old.

I suppose an alternative would be to compress just the JSON document using a compression utility. I've tried LZ-String but I can't seem to be able to compress the document on the server using the C# version and decompress on the client using the JavaScript version, and have the resulting JSON document be recognized.

Randy Minder
  • 47,200
  • 49
  • 204
  • 358
  • Please look at following link. http://stackoverflow.com/questions/2775261/how-to-enable-gzip-http-compression-on-windows-azure-dynamic-content – Erdogan Kurtur Sep 05 '14 at 12:55
  • Thanks but that question is over 4 years old and I'm not using web roles. – Randy Minder Sep 05 '14 at 13:11
  • so, where in your question you stated what service are you using? From your comment seems that is is Azure Web Sites. With your reputation you should know this is fairly important to mention the exact configuration describing your issue... I believe that this article will be in help: http://azure.microsoft.com/blog/2014/01/28/more-to-explore-configuration-options-unlocked-in-windows-azure-web-sites/ – astaykov Sep 05 '14 at 13:39
  • And this one: http://bartwullems.blogspot.de/2013/03/enable-dynamic-compression-for-aspnet.html – astaykov Sep 05 '14 at 13:44

1 Answers1

14

To enable compression of JsonResult of your MVC Controller's actions you need to enable dynamic compression from web.config file:

  <system.webServer>
    <urlCompression doDynamicCompression="true" />
    <httpCompression>
      <dynamicTypes>
        <add mimeType="application/json" enabled="true" />
        <add mimeType="application/json; charset=utf-8" enabled="true" />       
      </dynamicTypes>
    <staticTypes>
        <add mimeType="application/json" enabled="true" />
        <add mimeType="application/json; charset=utf-8" enabled="true" />       
    </staticTypes>
    </httpCompression>
  </system.webServer>  

A working example with exact above configuration is published on free tier of Azure WebSites and can be tested with simple HTTP GET request:

GET https://double.azurewebsites.net/Home/SomeJson HTTP/1.1
User-Agent: Fiddler
Accept-Encoding: gzip, compress
Host: double.azurewebsites.net

Note, that Accept-Encoding header is absolute must to trigger server side compression. Please also note the mime type application/json; charset=utf-8 which is the mime type served by the ASP.NET MVC5 JsonResult.

Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
astaykov
  • 30,768
  • 3
  • 70
  • 86
  • This looks good. How does this work with content negotiation in Web API? The Accept header is what usually determines what is served back. Also, is the JSON automatically decompressed by the browser? All browsers? – JasonCoder Sep 05 '14 at 14:37
  • Great question. I need to have all the major browsers automatically do a decompression on this, or I can't use it. – Randy Minder Sep 05 '14 at 16:29
  • Randy Minder, we are in 21st century. all the (major) browsers don support compression. it is matter of configuration to enable the browser to send this headers. and you don't really want to always send compressed content - just for the case where the client cannot decompress it. these are called web standards. check out the web a little and do a small research on the accept-encoding headers before posting sarcastic comments! of course there are means to always compress the response regardless of the request headers - just when you want to be non-standards compliant. just search internet ... – astaykov Sep 06 '14 at 07:10
  • And even more, if you just open the URL https://double.azurewebsites.net/Home/SomeJson with any modern browser and use browsers tool for network monitoring you will see that all of them send these Accept Encoding by default. And the response is of course Compressed. And the decompression is done transparent by the browser, so you don't need to do absolutely anything in your JavaScript. – astaykov Sep 06 '14 at 07:54
  • @JasonCoder Negotiations with WebAPI are absolutely the same! You just have to include in the `dynamicTypes` the mime-types of the content that is required to be compressed. Rest is implementation of RFC for HTTP protocol which is implemented by both Browsers and Servers ... – astaykov Sep 06 '14 at 07:55