Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio
Asp.Net Core

ASP.NET Core: Fix Output Caching That Serves the Same Response to Every User Regardless of Query String

- 27.09.26 - ErcanOPAK

⚙️ The Cache That Couldn’t Tell One Query String From Another

ASP.NET Core’s built-in Output Caching middleware caches a response keyed on the request path by default – it does NOT automatically vary the cached entry by query string parameters unless a policy explicitly says to. That means an endpoint like /products?category=shoes and /products?category=hats can end up sharing a single cached response, silently serving one category’s results to visitors asking for the other, with no error anywhere in the pipeline.

🔎 The Problem

builder.Services.AddOutputCache();

var app = builder.Build();
app.UseOutputCache();

app.MapGet("/products", (string category) => GetProducts(category))
   .CacheOutput();  // No VaryByQuery configured

// First request: GET /products?category=shoes -> cached under key "/products"
// Second request: GET /products?category=hats -> cache HIT on the same
// key, returns the SHOES response instead of running the handler again.
// The category parameter is completely invisible to the cache key.

✅ Fix: Vary the Cache Explicitly by Every Parameter That Changes the Response

  • Calling .CacheOutput(policy => policy.SetVaryByQuery(“category”)) tells the middleware to include that query parameter as part of the cache key, so each distinct value gets its own cached entry instead of colliding with every other value on the same path.
  • A shared, named policy (registered once via AddOutputCache(options => options.AddPolicy(…))) applied consistently across every endpoint that varies by query string is safer than remembering SetVaryByQuery on each individual endpoint definition one at a time.
  • For an endpoint where the full set of query parameters that affect output isn’t obvious at a glance, VaryByValue with a custom key-building function is worth the extra code – it guarantees the cache key reflects everything that actually changes the response, rather than trusting a manually maintained list of parameter names to stay complete.

⚠️ Why This Is Easy to Ship Without Noticing

  • The bug is invisible in local testing specifically because a single developer testing one category at a time never populates the cache with a competing entry to collide with – it only appears once real traffic hits the endpoint with more than one distinct query string value within the cache’s expiration window.
  • This is a newer failure mode than response caching bugs from older ASP.NET Core versions, precisely because Output Caching (introduced in .NET 7) is opt-in per endpoint via .CacheOutput() – a team migrating existing endpoints to use it can easily carry over the assumption that caching ‘just handles’ query strings the way response caching headers used to, when it explicitly requires a VaryBy configuration instead.

A cache key that ignores the query string isn’t caching the endpoint – it’s caching whichever request happened to arrive first, and handing that same answer to everyone who asked something different.

— Backend Architect

Related posts:

.NET Core: Implement Factory Pattern with DI

.NET Core: Implement Factory Pattern with DI

.NET Core Dependency Injection “Multiple Registrations” Bug

.NET Core Dependency Injection “Multiple Registrations” Bug

.NET Core: Use Middleware to Handle HTTP Requests

.NET Core: Use Middleware to Handle HTTP Requests

Views: 13
Found this useful? Share it
XLinkedInWhatsApp

Post navigation

WordPress: Fix a REST API Endpoint That Keeps Returning Cached Data After You Update a Post
Kubernetes: Fix a Liveness Probe That Kills Healthy Pods Under Normal Load

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Most Read

  1. 01How to make theater mode the default for Youtube 1,079 views
  2. 02How to add default value for Entity Framework migrations for DateTime and Bool 997 views
  3. 03Get the User Name and Domain Name from an Email Address in SQL 996 views
  4. 04Get the First and Last Word from a String or Sentence in SQL 873 views
  5. 05How to select distinct rows in a datatable in C# 868 views
  6. 06How to enable, disable and check if Service Broker is enabled on a database in SQL Server 671 views
  7. 07Add Constraint to SQL Table to ensure email contains @ 631 views
  8. 08Average of all values in a column that are not zero in SQL 584 views
  9. 09How to use Map Mode for Vertical Scroll Mode in Visual Studio 562 views
Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
Back to top
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com