dotnet / dotnet/aspnetcore

Add a ResponseCompression action filter

Open
#54,493 0 comments 0 reactions 0 assignees View on GitHub
api-suggestion area-mvc
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

## Background and Motivation

I am working on a project where we serve a lot of data to a frontend client and we want to enable response compression by using AspNetCore Response Compression. This is already possible by adding ResponseCompression services and adding ResponseCompression middleware. Due to CRIME or BREACH vulnerabilities we want only to add compression to specific endpoints which is possible by using MapWhen API. When using controllers and action we feel it is a of magic that we need to test against a route in Program when we define our dynamic routes in controller actions with HttpGet / Route attributes which makes it more error prone in case we need to move routes to a different path.

So I would suggest a ResponseCompressionAtrribute / ActionFilter that implements the same behavior as the ResponseCompressionMiddleware.

## Usage Examples

Usage of this new action filter

```csharp
[Route("api")]
public class HeavyDataController : BaseController
{
[HttpGet("heavy-data")]
[ResponseCompression]
public IActionResult GetLargeAmountOfData()
{
// omit fetching large amount of data

return Ok();
}
}
```

I have tried looking into implementing this myself in that one project but a lot of classes and providers in ResponseCompresssion library are internal so it is not possible for me to implement a filter with current library design. Another way could be open these implementations so it is possible for us to reuse the logic.

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.