WebAPI Gzip when returning HttpResponseMessage

后端 未结 4 542
心在旅途
心在旅途 2021-01-30 09:13

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;
u         


        
4条回答
  •  抹茶落季
    2021-01-30 09:39

    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:

    • NuGet package page
    • GitHub

    Hope that helps.

    **Updated after comment from @JCisar

    Update for ASP.Net Core

    Nuget Package is

    Microsoft.AspNetCore.ResponseCompression

提交回复
热议问题