12

I have compression enabled within IIS7 and it works as expected on all responses except for those constructed by ASP.NET AJAX. I have a web service that provides data to the client. When the web service is called directly, it is properly compressed. However, when it is called via ASP.NET AJAX, the JSON response is not compressed.

How can I get ASP.NET AJAX to send its JSON response with GZip compression?

4 Answers4

5

IIS7 uses the content-encoding to decide whether to compress the response (assuming of course that the browser can accept gzip). They're set in applicationHost.config, and by default the list is

<dynamicTypes>
     <add mimeType="text/*" enabled="true" />
     <add mimeType="message/*" enabled="true" />
     <add mimeType="application/x-javascript" enabled="true" />
     <add mimeType="*/*" enabled="false" />
</dynamicTypes>

If you call the web service directly, the XML response has a content-type of text/xml, which gets compressed. When called by AJAX, the JSON response has a content type of application/json, so it isn't compressed. Adding the following to applicationHost.config should fix that...

     <add mimeType="application/json" enabled="true" />
stevemegson
  • 11,843
  • 2
  • 38
  • 43
  • How can we do it in IIS6? – LCJ Sep 11 '14 at 02:21
  • And [Customizing the File Types IIS Compresses (IIS 6.0)](http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/5bce429d-c4a7-4f9e-a619-5972497b932a.mspx?mfr=true) – LCJ Sep 11 '14 at 02:31
1

What browser are you using? There's a bug in IE 6 that causes errors in compression. So ASP.NET AJAX turns off compression to IE 6 browsers:

http://weblogs.asp.net/scottgu/archive/2005/06/28/416185.aspx

Also, did you enable compression for ASMX files?

Keltex
  • 26,220
  • 11
  • 79
  • 111
0

Last I checked, the gzipping was something that IIS does (when setup correctly) - and of course when the browser sends the required headers

Sugendran
  • 2,099
  • 1
  • 14
  • 15
0

In general you don't want to do this unless you wouldn't mind throwing orders of magnitudes the amount of server power into your apps...

Also not only server-CPU but also client-CPU becomes a problem when you do this....

This concludes with that your app becomes WAY slower if you GZip all your Ajax Responses...!

Thomas Hansen
  • 5,523
  • 1
  • 23
  • 28
  • The client side emphasis is very important to note. In addition, be mindful of when to compress and when to not. In fact, there are times where compression actually makes the object larger before decompression. Using MVC, it can be done in any ASP.NET app, I created an annotion for my controller actions by creating a class that inherits from ActionFilterAttribute. Within the class I pull the accept-encoding header, filterContext.HttpContext.Request.Headers["Accept-Encoding"], and check for deflate or gzip. If they are present and meet my business specific conditions I deflate or GZipStream. – Anthony Mason Apr 27 '15 at 19:09