Introduce ability to disable response compression #58737
Open
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;
};
});