I have a WebAPI controller that returns an HttpResponseMessage
and I want to add gzip compression. This is the server code:
using System.Net.Http; using System.Web.Http; using System.Web; using System.IO.Compression; [Route("SomeRoute")] public HttpResponseMessage Post([FromBody] string value) { HttpContext context = HttpContext.Current; context.Response.Filter = new GZipStream(context.Response.Filter, CompressionMode.Compress); HttpContext.Current.Response.AppendHeader("Content-encoding", "gzip"); HttpContext.Current.Response.Cache.VaryByHeaders["Accept-encoding"] = true; return new SomeClass().SomeRequest(value); }
And this is the client code for the ajax call, using jquery:
$.ajax({ url: "/SomeRoute", type: "POST", cache: "false", data: SomeData, beforeSend: function (jqXHR) { jqXHR.setRequestHeader('Accept-Encoding', 'gzip'); }, success: function(msg) { ... }
When I run this, the server code returns without bugging but the client bugs:
(failed) net::ERR_CONTENT_DECODING_FAILED
When I look with Fiddler, this is what I see:
What do I need to change to make the web service return gzipped content that the client processes normally? I know I could also do this with an HttpModule or through some setting on IIS but neither option fits the scenario of the hosting:
Please note that I'm not looking for an IIS setting because I don't have access to that (hosting).
Then use the following class at the method level as in the following: . Execute the Web API method by commenting the DeflateCompression attribute that is applied on the Web API controller method, you will find that returned data is not compressed but simple JSON. See the execution time when compression is not applied.
When you're unable to use the compression features of web servers (IIS, Apache, Nginx), ASP.NET Core provides an alternate option, Response Compression middleware.
Enabling GZip compression in Spring Boot GZip compression is a very simple and effective way to save bandwidth and improve the speed of your website. It reduces the response time of your website by compressing the resources and then sending it over to the clients. It saves bandwidth by at least 50%.
server.compression.enabled= true. # Minimum response where compression will kick in. server.compression.min-response-size= 4096. # Mime types that should be compressed. server.compression.mime-types=text/html, text/xml, text/plain, text/css, text/javascript, application/javascript, application/json.
Add these NuGet packages:
Microsoft.AspNet.WebApi.Extensions.Compression.Server System.Net.Http.Extensions.Compression.Client
Then and add one line of code to App_Start\WebApiConfig.cs
:
GlobalConfiguration.Configuration.MessageHandlers.Insert(0, new ServerCompressionHandler(new GZipCompressor(), new DeflateCompressor()));
That will do the trick!
Details at:
Hope that helps.
**Updated after comment from @JCisar
Update for ASP.Net Core
Nuget Package is
Microsoft.AspNetCore.ResponseCompression
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With