dotnet / dotnet/aspnetcore

Output cache middleware does not vary by query params specified at runtime

Open
#47,251 2 comments 0 reactions 0 assignees View on GitHub
area-middleware 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

### Describe the bug

The current way to specify to vary by query params(using a custom cache policy) is this:

```c#

class CustomCachePolicy : IOutputCachePolicy
{
public ValueTask CacheRequestAsync(OutputCacheContext context, CancellationToken cancellation)
{
var httpContext = context.HttpContext;

context.AllowCacheLookup = true;
context.AllowCacheStorage = true;
context.AllowLocking = true;
context.EnableOutputCaching = true;
context.CacheVaryByRules.QueryKeys = "page";
```

However more dynamic scenarios require the query params to be specified during execution of a controller:

```c#

[OutputCache(PolicyName = "default")]
public IActionResult Index()
{
var ocFeature = this.HttpContext.Features.Get();

if (ocFeature != null)
{
ocFeature.Context.CacheVaryByRules.QueryKeys = new Microsoft.Extensions.Primitives.StringValues("page");
}

return this.View();
}
```

I would expect the page to vary by query params specified during an execution of a controller/handler.

### Expected Behavior

I would expect the page to vary by query params specified during an execution of a controller/handler.

### Steps To Reproduce

Open [this github project ](https://github.com/Gebov/aspnetcore-7-cache-issue)and run it. Then browse through the pages

### Exceptions (if any)

_No response_

### .NET Version

7.0.202

### Anything else?

_No response_

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.