dotnet / dotnet/aspnetcore

Unable to remove Output Cache defaults

Open
#45,339 1 comment 0 reactions 0 assignees View on GitHub
area-middleware feature-output-caching
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 5h
Merged PRs (30d)
276

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Describe the bug

I'm reading the article at: https://learn.microsoft.com/en-us/aspnet/core/performance/caching/output?view=aspnetcore-7.0#default-output-caching-policy. In particular, I'm doing some tests about the default caching policy. According to it, by default:

> Responses to authenticated requests aren't cached.

And then:

> The following code removes these defaults while applying caching to all of an app's endpoints:
>
> `builder.Services.AddOutputCache();`

However, even with the above code, the following request isn't cached:

```

app.UseOutputCache();

// ...

app.MapGet("/withcache", () => DateTime.UtcNow);

app.Run();
```

So, if I don't call the `.CacheOutput(`) method after `MapGet`, the endpoint isn't cached, even if the documentation tells the opposite (because I have used `builder.Services.AddOutputCache();`, that applies caching to all of an app's endpoints). Moreover, it doesn't work also if I add `CacheOutput()` and then `RequireAuthorization()`:

app.MapGet("/withcache", () => DateTime.UtcNow).CacheOutput().RequireAuthorization();

It seems that, despite the documentation, defaults aren't removed at all. The only way I manage to use cache output for authenticated requests is providing a custom `IOutputCachePolicy `implementation.

### Expected Behavior

According to the documentation, using `builder.Services.AddOutputCache()`; all the output caching defaults should be removed, so authenticated request should be cached as well.

### Steps To Reproduce

Minimal repro here: https://github.com/marcominerva/OutputCacheIssue.

### Exceptions (if any)

_No response_

### .NET Version

7.0.100

### 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.