ASP.NET Core Output Caching Not Working? .NET 10 Fixes
Last updated: November 2, 2026 · Tested with .NET 10.0.12 (SDK 10.0.401), ASP.NET Core 10.0.12, Windows 11
Short answer: output caching stores the whole response on the server, so repeat requests skip your endpoint. In my test a 60 ms endpoint dropped to about 1 ms. If it isn’t working, the usual cause is one of three things: UseOutputCache() is missing, it runs before UseRouting(), or the response is one the default policy refuses, such as a POST, a 404 or anything with a cookie. All of those fail silently, with no error.
| Symptom | Cause | Fix |
|---|---|---|
| Handler runs on every request, no error | UseOutputCache() missing |
Add it |
Same, and you call UseRouting() yourself |
UseOutputCache() runs before routing |
Move it after UseRouting() |
| Same, only on some endpoints | Not a cacheable response: POST, non-200, Set-Cookie, Authorization header |
Expected; see “What the default policy refuses” |
Unable to resolve service for type ... IOutputCacheStore |
AddOutputCache() missing |
Add it |
| Everyone gets the first user’s data | Custom policy without a user in the key | Vary by user |
| Anonymous users get a protected page | Custom policy and cache before auth | Put UseOutputCache() after UseAuthorization() |
| Update visible on one server only | Default store is per process | Shared store such as Redis |
How I Tested It
One console project starts a real Kestrel server for each of 26 scenarios and calls it over HTTP. Handlers count their own calls, so a cache hit shows up as a counter that doesn’t move. The Redis section is the exception: I had no Redis server here, so that code is compile-checked and I only confirmed the store type it registers.
Basic Setup
Two lines in the pipeline and one on the endpoint:
builder.Services.AddOutputCache();
app.UseOutputCache();
app.MapGet("/products", async () => { await Task.Delay(50); return $"calls={++calls}"; }).CacheOutput();
The Task.Delay(50) stands in for a database query. Without caching, the handler ran on all 5 requests. With it, the handler ran once:
request 1: 105.1 ms calls=1
request 2: 2.9 ms calls=1
request 3: 1.0 ms calls=1
request 4: 0.8 ms calls=1
request 5: 0.6 ms calls=1
handler ran: 1
The uncached version settled at about 63 ms per request, so a hit was roughly 60 times faster. The numbers come from localhost, and the first request includes startup work, so look at the difference, not the exact values.
On a controller, use the attribute:
[OutputCache(Duration = 60)]
public string Cached() => $"cached calls={++cached}";
To cache every endpoint without touching each one, set a base policy. In my test a plain MapGet with no CacheOutput() was cached:
builder.Services.AddOutputCache(options =>
{
options.AddBasePolicy(policy => policy.Expire(TimeSpan.FromSeconds(30)));
});
Then opt single endpoints out with [OutputCache(NoStore = true)]; that handler ran on all 3 requests. The default lifetime is 60 seconds, and Expire changes it. A 1-second policy served the cached copy immediately and a new one after 1.2 seconds.
Why It’s Not Working
Three mistakes, and only one gives you an error.
Forgot AddOutputCache(). This one is loud, at startup:
InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.OutputCaching.IOutputCacheStore' while attempting to activate 'Microsoft.AspNetCore.OutputCaching.OutputCacheMiddleware'.
Forgot UseOutputCache(). The endpoint still has .CacheOutput(), the app runs fine, and nothing is cached:
200 calls=1
200 calls=2
200 calls=3
handler ran: 3
Wrong order with UseRouting(). This gave the same result, again with no message. The middleware reads the policy from the matched endpoint, and before routing there is no endpoint yet:
app.UseOutputCache();
app.UseRouting();
If you don’t call UseRouting() yourself, this doesn’t apply. If you do, put UseOutputCache() after it.
What the Default Policy Refuses
I sent 3 requests of each kind, all with .CacheOutput():
POST ran 3, 404 ran 3, Set-Cookie ran 3, Authorization header ran 3, GET then HEAD ran 1
So POST, a 404, a response that sets a cookie, and a request with an Authorization header were never served from the cache. A HEAD request was answered from the cached GET. This matters when you debug: test with Swagger or Postman while logged in and the Authorization header turns caching off, so it can look broken when it’s working. And if a session or antiforgery cookie is set on every response, nothing gets cached.
Output Caching vs Response Caching
The client controls response caching; the server controls output caching. I sent Cache-Control: no-cache to both after the first request:
200 calls=1
with Cache-Control: no-cache -> calls=1
200 calls=1
200 calls=1
with Cache-Control: no-cache -> calls=2
Output caching (first block) ignored the header and served the cache. Response caching (second block) ran the handler again. So with response caching, anyone can force your slow query by sending one header. Output caching also adds tags, locking and pluggable storage. Response caching needs the response to carry Cache-Control: public, which also tells browsers and proxies to cache it. I’d use output caching for an API unless you specifically want HTTP caching in clients.
Vary the Cache by Query and Header
By default the whole query string is part of the key, which has a cost. Here tracking parameters each created a new entry, so one utm_source value is one more handler run:
/all?page=1 -> calls=1 ?page=1
/all?page=2 -> calls=2 ?page=2
/all?utm_source=x&page=1 -> calls=3 ?utm_source=x&page=1
/all?page=1&b=2 -> calls=4 ?page=1&b=2
/all?b=2&page=1 -> calls=4 ?page=1&b=2
/all?PAGE=1 -> calls=1 ?page=1
/ALL?page=1 -> calls=1 ?page=1
Parameter order and case didn’t matter. Note the last lines: ?PAGE=1 got the cached ?page=1 response, so if your API treats values or names as case-sensitive, that’s worth knowing. To use only the parameters that change the result:
o.AddPolicy("ByPage", p => p.SetVaryByQuery("page"));
/paged?page=1 -> calls=5 ?page=1
/paged?page=2 -> calls=6 ?page=2
/paged?page=1&utm_source=x -> calls=5 ?page=1
Headers are not in the key by default. A French user got the English response until I added the header:
/plain en -> calls=1 lang=en
/plain fr -> calls=1 lang=en
o.AddPolicy("ByLang", p => p.SetVaryByHeader("Accept-Language"));
/lang en -> calls=2 lang=en
/lang fr -> calls=3 lang=fr
/lang en -> calls=2 lang=en
Invalidate With Tags
Time-based expiry alone means stale data until it runs out. Tag the entries, then evict the tag when data changes:
o.AddPolicy("Products", p => p.Expire(TimeSpan.FromMinutes(10)).Tag("products"));
app.MapPost("/products", async (IOutputCacheStore cache, CancellationToken ct) =>
{
await cache.EvictByTagAsync("products", ct);
return Results.Created();
});
/products -> calls=1
/products/7 -> id=7 calls=2
/products -> calls=1
POST /products evicted the tag
/products -> calls=3
/products/7 -> id=7 calls=4
One call cleared both the list and the single item, because they share a tag.
Stampede Protection
When a popular entry expires, many requests can arrive at once. Output caching locks by default, so one request does the work and the rest wait for it. I sent 20 parallel requests to an empty cache:
SetLocking(True): handler ran 1 of 20
SetLocking(False): handler ran 20 of 20
That’s a free feature you’d otherwise have to build. Leave it on.
Size Limits Fail Silently Too
MaximumBodySize (64 MB by default) and SizeLimit (100 MB by default) both skip caching without saying so. I set small limits to see it:
2 KB body, 1 KB limit: handler ran 3 of 3
/a ran 1 of 3, /b ran 3 of 3 (limit 2 KB)
When the cache was full, the existing entry stayed and the new one was never stored. If caching stops working only after the app has been up for a while, check this.
The Mistake That Leaks Data
The default policy won’t cache authenticated requests. With .CacheOutput() on a protected endpoint, an anonymous user got a 401 in both middleware orders, so plain CacheOutput() was safe in my tests.
The danger starts when you write a custom policy to cache logged-in requests, usually because of the “nothing is cached” behaviour above. A policy like that sets these flags:
context.EnableOutputCaching = true;
context.AllowCacheLookup = true;
context.AllowCacheStorage = true;
context.AllowLocking = true;
Nothing there adds the user to the cache key. So the second user got the first user’s data, with a 200:
alice -> 200 secret data of alice
bob -> 200 secret data of alice
The fix is to vary by user:
context.CacheVaryByRules.VaryByValues["user"] = context.HttpContext.User.Identity?.Name ?? "";
alice -> 200 secret data of alice
bob -> 200 secret data of bob
alice -> 200 secret data of alice
Middleware order is the second half of this. With that same custom policy and UseOutputCache() placed before authentication, a cached response skips the auth check completely. An anonymous visitor got the admin page:
app.UseRouting();
app.UseOutputCache();
app.UseAuthentication();
app.UseAuthorization();
alice -> 200 admin report
anonymous -> 200 admin report
Reverse the request order and it fails the other way. The policy cached the 401, and the logged-in user was locked out:
anonymous -> 401
alice -> 401
Two fixes, and you need both. Put the cache after auth:
app.UseAuthentication();
app.UseAuthorization();
app.UseOutputCache();
And don’t store anything but a 200 in your policy:
public ValueTask ServeResponseAsync(OutputCacheContext context, CancellationToken ct)
{
if (context.HttpContext.Response.StatusCode != StatusCodes.Status200OK)
context.AllowCacheStorage = false;
return ValueTask.CompletedTask;
}
alice -> 200 admin report
anonymous -> 401
alice -> 200 admin report
Multiple Servers: Redis
The default store lives in one process. I ran two instances, as behind a load balancer, and sent the update to the first:
instance 1: version=1
instance 2: version=1
POST on instance 1
instance 1: version=2
instance 2: version=1
Instance 2 keeps serving old data until its entry expires. A shared store fixes that. Install Microsoft.AspNetCore.OutputCaching.StackExchangeRedis (10.0.14) and register it:
b.Services.AddStackExchangeRedisOutputCache(o => o.Configuration = "localhost:6379");
That registers the Redis store in place of the memory one. As I said at the top, I compiled this and checked the registered store type, but I didn’t run it against a Redis server, so test eviction across your own instances before relying on it.
Checklist
AddOutputCache()andUseOutputCache()are both there, afterUseRouting()if you call it.UseOutputCache()comes afterUseAuthentication()andUseAuthorization().- Test without an
Authorizationheader, and check that no cookie is set on the response. - Use
SetVaryByQuerywith only the parameters that matter. - Add every header that changes the response with
SetVaryByHeader. - Tag entries, and call
EvictByTagAsyncafter every write. - In a custom policy for logged-in users, vary by user and store only 200s.
- Running more than one instance? Use a shared store.
Related: build a REST API with ASP.NET Core · JWT authentication: 401 errors fixed · Minimal APIs · all ASP.NET Core tutorials