百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
M

Microsoft.AspNet.WebApi.MessageHandlers.Compression

> 编程语言
开源

ASP 投放模块. 启用 GZip 和 Deflate 支持的网络网络API

194 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

ASP 投放模块. 启用 GZip 和 Deflate 支持的网络网络API

Compression support for ASP.NET WebAPI and HttpClient

Drop-in module for ASP.Net WebAPI that enables GZip and Deflate support.
This module is based on this blog post by Ben Foster which in turn is based on this blog post by Kiran Challa.
This code improves on their work by adding several new options, as well as fixing some issues with the original code.

Package Version Downloads
Microsoft.AspNet.WebApi.Extensions.Compression.Server
System.Net.Http.Extensions.Compression.Client

How to use

Server side

You need to add the compression handler as the last applied message handler on outgoing requests, and the first one on incoming requests.
To do that, just add the following line to your App_Start\WebApiConfig.cs file after adding all your other message handlers:

csharp
GlobalConfiguration.Configuration.MessageHandlers.Insert(0, new ServerCompressionHandler(new GZipCompressor(), new DeflateCompressor()));

This will insert the ServerCompressionHandler to the request pipeline as the first on incoming requests, and the last on outgoing requests.

Client side

JavaScript

If you are doing your requests with JavaScript you probably don't have to do do anything.
Just make sure the gzip and deflate values are included in the Accept-Encoding header. (Most browsers do this by default)

C#

You need to apply the following code when creating your HttpClient.

csharp
var client = new HttpClient(new ClientCompressionHandler(new GZipCompressor(), new DeflateCompressor()));

client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip"));
client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("deflate"));

Thats it! You should now immediately start experiencing smaller payloads when doing GET, POST, PUT, etc.

Advanced use

Skip compression of requests/responses that are smaller than a specified value

By default, both ServerCompressionHandler and ClientCompressionHandler compress everything larger than 860 bytes.
However, this can be overriden by inserting a threshold as the first parameter like this:

csharp
var serverCompressionHandler = new ServerCompressionHandler(4096, new GZipCompressor(), new DeflateCompressor());
var clientCompressionHandler = new ClientCompressionHandler(4096, new GZipCompressor(), new DeflateCompressor());

The above code will skip compression for any request/response that is smaller than 4096 bytes / 4 kB.

Disable compression for endpoint

It is possible to disable compression for a specific endpoint. Just add the [Compression(Enabled = false)] attribute to your endpoint method. (Or the whole controller if you want to disable for all endpoints in it)

OWIN Authentication

When using the OWIN Authentication pipeline, you might encounter errors saying that Server cannot append header after http headers have been sent. This is a bug with OWIN, but as of this moment it has not been fixed.

The workaround is to install the Microsoft.AspNet.WebApi.Extensions.Compression.Server.Owin package and use the included OwinServerCompressionHandler instead of the default ServerCompressionHandler. This class contains code to detect whether the headers have been sent already and prevent any attempts at compression.

Version history

2.0.3 (current)

  • Fixed unnecessary dependency on .NET 4.5.2
  • Fixed System.ArgumentException in CompressionAttribute (Thanks to SneakyMax)
  • Various readme fixes (Thanks to wiltodelta, IainCole, coni2k and altumano)

2.0.0

  • Fixed UWP projects referencing Microsoft.AspNet.WebApi.MessageHandlers.Compression does not compile
  • Fixed Remove Microsoft.Bcl dependency in .NET 4.5
  • Fixed compressing even when accept encoding is null
  • Fixed Possible issue while redirecting after de-compression and Owin UseCookieAuthentication does not work anymore after we insert ServerCompressionHandler
  • Fixed Async throttled actions with disabled compression
  • Improved performance a little by not always buffering the response. See #6
  • Fixed Server cannot append header after HTTP headers have been sent. NOTE: The fix is in a separate package

1.3.0

  • Added attribute for disable compression for certain routes
  • Fixed clearing of non-standard properties when compressing and decompressing

1.2.2

  • Stop trying to compress requests/responses with no content

1.2.1

  • Properly copy HTTP headers from the content that is going to be compressed

1.2.0

  • Fixed 504 timeout error when returning ByteArrayContent from an ApiController
  • Fixed bug with content stream sometimes being disposed before returning
  • Added better test coverage

1.1.2

  • Changed default threshold for compression to 860 bytes (what Akamai uses)
  • Now reports proper Content-Length

1.1.0

  • Simplified usage
  • Added support for setting a minimum content size for compressing

1.0.3

  • First release, basic compression of server responses and client requests
  • Did not support compressing POSTs and PUTs

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

C#

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言