dotnet / dotnet/aspnetcore

Introduce ability to disable response compression

Open
#58,737 1 comment 1 reaction 0 assignees View on GitHub
api-suggestion area-middleware
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

## Background and Motivation

Sometimes, we may want to avoid response compression for some endpoints, such as small responses; it may cause bad performance for small responses, and some may want to get rid of response compression.

## Proposed API

```diff
namespace Microsoft.AspNetCore.Http.Metadata;

+ public interface IDisableResponseCompressionMetadata
+ {
+ }
```

```diff
namespace Microsoft.AspNetCore.Http;

+ public sealed class DisableResponseCompressionAttribute : Attribute, IDisableResponseCompressionMetadata
+ {
+ }

+ public static class HttpResponseCompressionEndpointConventionBuilderExtensions
+ {
+ public static IEndpointConventionBuilder DisableResponseCompression(this IEndpointConventionBuilder builder);
+ }
```

## Usage Examples

```cs
app.UseResponseCompression();
app.MapGet("/deploy-info", (IOptions option) => Results.Ok(option.Value)).DisableResponseCompression();
app.MapMetrics().DisableHttpMetrics().DisableResponseCompression();
```

## Alternative Designs

```diff
namespace Microsoft.AspNetCore.ResponseCompression;

public class ResponseCompressionOptions
{
+ public Func> Predict { get; set; }
}
```

```c#
services.AddResponseCompression(options =>
{
options.Predict = context =>
{
var noCompressionExists = context.Request.Headers.TryGetValue("no-compression", out _);
return noCompressionExists is not true;
};
});
```

```c#
services.AddResponseCompression(options =>
{
options.Predict = context =>
{
var contentLength = context.Response.ContentLength;
return contentLength.HasValue && contentLength.Value > 1024;
};
});
```

## Risks

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.