MVC4 WebAPI not compressing GET responses
Asked Answered
B

2

11

I'm working on a project that uses the MVC4 WebAPI (RC). The responses are not gzip compressed (dynamic compression in IIS is enabled).

The responses from normal MVC controllers are compressed. Do I need a specific setting to enable gzip compression for WebAPI responses?

I could add a custom compression handler, but if possible, I would just like to use the built-in IIS compression.

BTW, I know this is almost a duplicate of Compress HTTP GET Response, however the accepted answer there doesn't really answer my question.

Burley answered 8/6, 2012 at 13:3 Comment(4)
Is dynamic compression enabled for mimetype 'application/json; charset=utf-8'? By default this is not enabled even if dynamic compression is enabledMinimal
@EivindT how can I check that?Burley
You can look in the applicationhost.config file under %windir%\System32\inetsrv\config in the <httpCompression><dynamicTypes> section. You should not edit the file, instead use appcmd.exe to change it like this: https://mcmap.net/q/830915/-how-to-enable-gzip-http-compression-on-windows-azure-dynamic-contentMinimal
Cool, that works! If you'll post this as an answer I will accept it.Burley
M
11

Is dynamic compression enabled for mimetype application/json; charset=utf-8? By default this is not enabled even if dynamic compression is enabled.

To see if it is enabled, you can look in the applicationhost.config file under %windir%\System32\inetsrv\config in the section.

You should not edit the file, instead use appcmd.exe to change it like this: https://mcmap.net/q/830915/-how-to-enable-gzip-http-compression-on-windows-azure-dynamic-content

Minimal answered 26/7, 2012 at 15:7 Comment(1)
I had no problem editing the applicationhost.config file manually using notepad. All you need to add is one line to the httpCompression -> dynamicTypes section: <add mimeType="application/json" enabled="true" />. See this link for more details: benfoster.io/blog/aspnet-web-api-compression Once I did that, my web api responses were gzipped.Megdal
F
0

Actually you can install a nuget package to achieve that, this requires little configuration, in client side you just need to add the accept-encoding:gzip.

Using this approach is better these days when ASP.Net is able to run in any Web Server.

In this video I have a full / complete explanation of WebApi using gzip compression. Is in spanish, but you know "Code is an universal language"

NinjaTip #16 | Incluyendo soporte para GZip en WebAPI | C# NinjaTip #16 | Incluyendo soporte para GZip en WebAPI

Filibeg answered 8/6, 2012 at 13:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.