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 Response Caching Ignoring a Vary Header

- 28.11.26 - ErcanOPAK

⚙️ Fix Response Caching Ignoring a Vary Header

The built-in Response Caching Middleware keys its cache entries off the response’s own Vary header, not off some separate configuration the middleware maintains independently – so whatever Vary value happened to be present on the first response cached for a given URL is the one every subsequent request is bucketed against, for as long as that entry lives. If the header that actually varies the response is only set conditionally in application code – Vary: Accept-Language added only when a translation exists, and omitted on a fallback to the default language – the very first cache miss for that URL can be exactly the request that falls into the fallback branch, caching a response with no Vary header at all, and every later request regardless of its own Accept-Language gets served that same cached default.

🔎 The Problem

[HttpGet("/welcome")]
[ResponseCache(Duration = 300)]
public IActionResult Welcome()
{
    var lang = Request.Headers.AcceptLanguage.ToString();
    if (_translations.TryGetValue(lang, out var text))
    {
        Response.Headers.Vary = "Accept-Language"; // only set on this branch
        return Content(text);
    }
    return Content(_translations["en"]); // fallback: Vary never set here
}

// First request ever made: Accept-Language: tr-TR, no Turkish translation
// yet configured -> falls to the "en" branch -> cached WITHOUT a Vary
// header at all
// Every later request (Accept-Language: de-DE, fr-FR, whatever) hits the
// same cache entry and gets the English fallback text, because the
// middleware has no Vary header on record telling it to key by language

✅ Fix: Set Vary on Every Code Path

  • Set the Vary header unconditionally, on every code path through the action including the fallback branch, so the middleware’s cache key always accounts for Accept-Language regardless of which branch happened to produce the very first cached response.
  • Alternatively, configure [ResponseCache(VaryByHeader = “Accept-Language”)] directly on the attribute, which applies the Vary behavior declaratively at the action level rather than depending on the action remembering to set the header on every branch.
  • When adding response caching to any endpoint whose output depends on a request header, write a test that hits the endpoint twice with two different values of that header, before and after a cache is warm, and assert the two responses actually differ.

🚨 Why This Is Dangerous

A caching bug that depends on which request happens to arrive first is close to impossible to reproduce reliably – it can pass every manual test, where you naturally try the common case first, and only surface in production once real traffic in an unpredictable order populates the cache with the wrong entry.

Response Caching Middleware isn’t ignoring the Vary header – it’s only ever as good as whichever response happened to set it first, for every request after that one.

— Backend Architect

Related posts:

Is Your Clean Architecture Actually a Dirty Mess?

Is Your Clean Architecture Actually a "Dirty" Mess?

Why BackgroundService Beats Task.Run in APIs

Why BackgroundService Beats Task.Run in APIs

How to get the current logged in user ID in ASP.NET Core

How to get the current logged in user ID in ASP.NET Core

Views: 8
Found this useful? Share it
XLinkedInWhatsApp

Post navigation

Git: Fix a Filter-Repo Run Orphaning an Old Tag Pointer
SQL Server: Fix a Table-Valued Function Forcing a Row Scan

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 561 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