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.

4

1 Answer 1

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.

5
  • 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
    Commented Sep 5, 2014 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. Commented Sep 5, 2014 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
    Commented Sep 6, 2014 at 7:10
  • And even more, if you just open the URL 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
    Commented Sep 6, 2014 at 7: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
    Commented Sep 6, 2014 at 7:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.