dotnet / dotnet/aspnetcore

Use Response Caching for non-200 status codes

Open
#29,978 2 comments 0 reactions 0 assignees View on GitHub
area-middleware enhancement feature-response-caching
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

### Description

I would like to enable Response Caching with the `ResponseCachingMiddleware` for some specific non-`200` responses. I would specifically need this for `404` responses, but I could imagine scenarios where this would be useful for some other status codes as well, such as `204`, `301`, `303`.

### Describe the solution you'd like

It would be great if this could be enabled with configuration via `ResponseCachingOptions`, for example if it contained a property like this:

```
public List EnabledStatusCodes { get; } = new List { HttpStatusCode.OK };
```

Which would be consistent with the current behavior, and it could be adjusted when registering it in the `Startup`:

```
services.AddResponseCaching(opts =>
{
opts.EnabledStatusCodes.Add(HttpStatusCode.NotFound)
});
```

But I'm okay with any other kind of solution too.

### Additional context

I hope I'm not missing anything related to this, and this is indeed not possible right now, I'm seeing this in the docs: https://docs.microsoft.com/en-us/aspnet/core/performance/caching/middleware?view=aspnetcore-5.0#conditions-for-caching, and in the code: https://github.com/dotnet/aspnetcore/blob/main/src/Middleware/ResponseCaching/src/ResponseCachingPolicyProvider.cs#L117

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.