dotnet / dotnet/aspnetcore

.NET 7.0: Output Caching: Evaluate policy after other middleware processed the request

Open
#44,191 2 comments 0 reactions 1 assignee Claimed by @sebastienros View on GitHub
area-middleware area-networking feature-output-caching
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Is your feature request related to a problem? Please describe the problem.

When providing APIs to be used publicly, some error code like `404` might worth to be cache longer than success code since we know this state won't change anytime soon.

In my case, I provide an API to search for subtitle for shows.

The api take the searched show from the URL. It's more than possible that said show isn't available on my service yet.
I return a 404 and like to cache that result for longer than a succesful found show with subtitles.

### Describe the solution you'd like

Delay evaluation of policy to when we have a response to let the developer use `HttpContext.Response` as part of rule for matching policy, like its status code.

### Additional context

Currently we have access to the response using the ` .With(context => context.HttpContext.Response.StatusCode == 404);` when defining a policy. The issue is, since the response isn't created yet, the status code is always 200.

Since there isn't yet a full documentation of the capabilities of the `With` method, it can leads to unexpected cases.

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.