Skip to content

Introduce ability to disable response compression #58737

Open
@WeihanLi

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

namespace Microsoft.AspNetCore.Http.Metadata;

+ public interface IDisableResponseCompressionMetadata
+ {
+ }
namespace Microsoft.AspNetCore.Http;

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

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

Usage Examples

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

Alternative Designs

namespace Microsoft.AspNetCore.ResponseCompression;

public class ResponseCompressionOptions
{
+   public Func<HttpContext, ValueTask<bool>> Predict { get; set; }
}
services.AddResponseCompression(options =>
{
    options.Predict = context => 
    {
        var noCompressionExists = context.Request.Headers.TryGetValue("no-compression", out _);
        return noCompressionExists is not true;
    };
});
services.AddResponseCompression(options =>
{
    options.Predict = context => 
    {
        var contentLength = context.Response.ContentLength;
        return contentLength.HasValue && contentLength.Value > 1024;
    };
});

Risks

Metadata

Assignees

No one assigned

    Labels

    api-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-middlewareIncludes: URL rewrite, redirect, response cache/compression, session, and other general middlesware

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions