Search code examples
c#asp.netswashbuckle

Add text compression to Swashbuckle API


I am developing an ASP.NET application, hosted by IIS. I have activated IIS text compression, so all the pages served are automatically GZipped (if client requesting them support that).

Unfortunately i don't find a way to tell Swashbuckle to apply compression to API responses, so that produced API responses are somewhat compressed.

Is it a feature supported by the tool? if so how should I proceede to achieve that?


Solution

  • I have solved the problem adding this nuget package :

    https://www.nuget.org/packages/Microsoft.AspNet.WebApi.Extensions.Compression.Server/2.0.6?_src=template

    And this two line of code in application start :

        protected void Application_Start(object sender, EventArgs e)
        {
            GlobalConfiguration.Configuration.MessageHandlers.Insert(0, new ServerCompressionHandler(contentSizeThreshold : 8192, new GZipCompressor(), new DeflateCompressor()));
            GlobalConfiguration.Configure(WebApiConfig.Register);
        }