ASP.Net Core 2.0 - ResponseCaching Middleware - Not Caching on Server core response caching not working core etag mvc cache-control header
web api 2 cache attribute
asp net core cache busting
cache middleware
asp net image cache

I want to use server-side response caching (output cache) with core 2.0 and found out about Response Caching Middleware and wanted to give it a try with a brand new asp.core mvc project.

Here is the description from the link above which makes me think this could be used like output cache.

The middleware determines when responses are cacheable, stores responses, and serves responses from cache.

Here is how my startup.cs looks like.

public class Startup
    public Startup(IConfiguration configuration)
        Configuration = configuration;

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)

        if (env.IsDevelopment())


        app.UseMvc(routes =>
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");

and here is the HomeController.cs

[ResponseCache(Duration = 60)]
public class HomeController : Controller
    public IActionResult Index()
        return View();

    public IActionResult About()
        ViewData["Message"] = "Your application description page.";

        return View();

    public IActionResult Contact()
        ViewData["Message"] = "Your contact page.";

        return View();

    public IActionResult Error()
        return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });

there is also a timestamp at the bottom of _Layout.cshtml file so i can tell when the page is rendered, like below.

<p>&copy; 2018 - ResponseCachingMiddleware - @DateTime.UtcNow</p>

Cache-Control headers seem to be fine, this is what I get in headers when I load the page but time stamp keeps getting updated on every refresh every second.


What I'm understanding from MS documentations is Response Caching Middleware is the server-side caching mechanism that takes care of caching the response while Response Caching seems to be just a filter to manipulate response headers for caching.

Can't tell if there is something wrong with my understanding or code and I wanna complain that I'm feeling this way too often since I started prototyping with ASP.Net Core. Maybe you could also suggest better resources as a side topic.

I've checked out this post before ASP.NET Core 2.0 - Http Response Caching Middleware - Nothing cached

Also checked this out but it seems like the only difference is I'm using mvc.


Edit: I'm seeing the message below in the output window, cannot find anything about it on google except the few places I already checked for response caching middleware.

Microsoft.AspNetCore.ResponseCaching.ResponseCachingMiddleware:Information: The response could not be cached for this request.

Note: I wish I could create #response-caching-middleware tag. Not sure #responsecache is relevant.

I had this same confusion recently.

ASP.Net Core's ResponseCaching does provide both client-side caching (through HTTP response headers) & server-side (through a memory cache'd middleware that short-circuits other middlewares if the response is in the cache). The server-side portion reads the HTTP response cache headers to determine if it should do server-side caching (similar to what an ISP or CDN might do).

Unfortunately, debugging the server-side ResponseCaching is tricky because it has weird rules & there's not adequate logging. In my case I pulled down Microsoft's source code to step through it & find the issue with my code.

The note you found in the output window "The response could not be cached for this request" is a clue.

There's 2 parts to the server-side caching of a request. The server has to prime the cache the first time the url is requested. It will serve the cached version the 2nd time it's requested. Pay attention to when the error message shows up, if it's on the 1st or 2nd request. That'll tell you if it couldn't be stored in the cache or if it couldn't be retrieved from the cache.

The rules for both storage & retrieval are in this source code file:

Your "Cache-Control:public,max-age=60" header should match these rules just fine.

My guess is you actually had it working, but didn't know how to test it correctly. There is a counter-intuitive portion of ResponseCaching noted in this issue: Essentially, if the browser sends a no-cache or no-store header (when you hit CTRL+F5 or have your debugger tools open), ASP.Net Core's ResponseCaching will honor the browser's request & re-generate the response.

So, to test if your code was working you probably loaded the page, which primed the cache, then you hit CTRL+F5 to force-refresh your browser & you expected the server-side to respond with a cached entry rather than running your WebAPI code. However, it honored the no-cache request header & bypassed the cache (& wrote that message in your output log).

The way to test this would be to clear your browser cache in-between requests (or switch to incognito), rather than using CTRL+F5.

On a side note, honoring the no-cache/no-store request headers was probably a poor design choice since ASP.Net Core's ResponseCache will most likely be used by a server who owns the response, rather than an intermediary cache like a CDN/ISP. I've extended the base ResponseCache with an option to disable honoring these headers (as well as serialize the cache to disk, rather than in-memory only). It's an easy drop-in replacement for the default cache.

You can find my extension here:

There are also a few other other gotchas with ResponseCaching to watch out for which you may have already read about in the blog urls you posted. Authenticated requests & responses with set-cookie won't be cached. Only requests using GET or HEAD method will be cached. If the QueryString is different, it'll make a new cache entry. Also, usually you'll want a "Vary" header to prevent caching if certain conditions of a request differ from the previously-cached request (example: user-agent, accept-encoding, etc). Finally, if a Middleware handles a request it'll short-circuit later Middlewares. Make sure your app.UseResponseCaching() is registered before app.UseMVC()

Response Caching Middleware in ASP.NET Core, › en-us › aspnet › core › performance › response On a side note, honoring the no-cache/no-store request headers was probably a poor design choice since ASP.Net Core's ResponseCache will most likely be used by a server who owns the response, rather than an intermediary cache like a CDN/ISP.

I had the same issue, I was about to pull my hairs over it, I'd set app.UseResponseCaching(); as well as services.AddResponseCaching(); and add ResponseCache on top of my action exactly like what was told in Microsoft official Docs, despite the the cache-controll header was set correctly on response returning from server but still nothing cached at server-side.

After couple of hours of sweating on this issue I figured out where the problem arises and why nothing cached at server.

Browsers by default set cache-controll value to max-age=0 for the request (if the request is not caused by back or forward) even though you set cache-controller correctly in your response by adding ResponseCache attribute on top of you action (or controller) since the cache-controller sent by request is set to max-age=0, the server is unable to cache response, I think this must be added to list of Response Caching limitation as well

Anyway you can override browser default behavior by adding few line of code right before calling app.UseResponseCaching(); on the other hand you need to add a custom middle-ware to modify request cache-control header value before calling app.UseResponseCaching();.

See code below, worked for me hope work for you too

 app.Use(async (ctx, next) =>
            ctx.Request.GetTypedHeaders().CacheControl = new Microsoft.Net.Http.Headers.CacheControlHeaderValue()
                Public = true,
                MaxAge = TimeSpan.FromSeconds(60)
            await next();

for ensuring that ResponseCaching works as expected you can also use postman but you must set 'Send no-cache Header' to off in the setting, see image below

Response caching in ASP.NET Core, Take advantage of response caching in ASP.Net Core to improve your application's performance by reducing the load on the web server. In Startup.Configure, Response Caching Middleware must be placed before middleware that require caching. For more information, see ASP.NET Core Middleware. The Authorization header must not be present. Cache-Control header parameters must be valid, and the response must be marked public and not marked private.

If the Cache-Control header is coming through, then it's working. That's all the server can do from that perspective. The client ultimately makes the decision whether or not to actually cache the resource. Sending the header doesn't force the client to do anything; in fact, the server, in general, cannot force the client to do anything.

How to use response caching middleware in ASP.Net Core, ASP.NET Core 2.0 Response Caching. Tahir Naushad; Updated date, To an empty project, configure services and middleware for caching in Startup, updated, i.e., it's not coming from the server but rather from the cache. Note that unlike output caching, response caching in ASP.Net Core doesn’t cache the responses in the memory of the web server. Response caching in ASP.Net Core is actually a better and

ASP.NET Core 2.0 Response Caching, NET Core MVC actions. Response caching does not cache responses on the web server. Output caching middleware is planned to be added to ASP.NET  Response caching in ASP.NET Core. Response caching reduces the number of requests a client or proxy makes to a web server. Response caching also reduces the amount of work the web server performs to generate a response. Response caching is controlled by headers that specify how you want client, proxy, and middleware to cache responses.

Response Caching, While response caching is a powerful technique, unless you have the ability to control but unfortunately the built-in response caching does not permit cache invalidation. We will implement the cache for a single web server using IMemoryCache. Fail to retrieve page from the cache; Execute inner middleware (the MVC  ASP.NET Core 2.0 Response Caching. As you navigate between a controller with caching enabled (Home) and another one without it (Movies), you’ll notice that time isn’t been updated i.e. it's not coming from server but rather from cache.

Custom response caching in ASP.NET Core (with cache invalidation), Unlike server side caching, it is not possible to instruct the browser to invalidate NET Core that we are going to discuss is the caching of data using In fact the built-in response caching middleware will not cache the page if any of the NET Core 2.0 runtime store on Linux · Dependency Injection in action filters in ASP. A guide to caching in ASP.NET Core. This post looks at the various techniques available in ASP.NET Core for caching. We'll look at caching of data, partial pages and full pages at the server and client level and explain when to use each.

  • Thank you for the detailed response. I've abandoned this problem for a while after finding a different solution but I'll check this out as soon as I got back to it. It makes more sense that I was testing it wrong than the functionality didn't exist.
  • @DevinGarner looked into your code and you should log exception(because there are). By the way, you should do a performance benchmark on your code compared to default response caching. Your implementation is quite slow :) and should support any IResponseCacheEntry. Regards.
  • Thanks for the code review @Fab if you have time, please submit a pull request with your improvements. Thanks!
  • Thank god I read your answer until the end. Calling app.UseResponseCaching(); before app.UseMvc(); did the trick!
  • Thank you, I've abandoned this problem for a while now but I'll get back to it shortly and give this a try.
  • @Engin happy coding :)
  • Yes it has worked, if some one have a compiling error add he namespace Microsoft.AspNetCore.Http GetTypedHeaders() is an extension method
  • Can you explain me what's the difference between Response Caching Middleware and Response Caching? As far I can tell, you're describing Response Caching filter's responsibility.
  • There is none. They are two sides of the same coin. The middleware is what actually does the work, while the attribute informs the middleware as to what it should actually do (i.e. set max-age=60 because you specified Duration = 60). Response caching, in general, only sets the Cache-Control header. It does nothing to cache the response server-side. And, like I said, it's 100% up to the client as to whether the header is actually honored. If you're looking for server-side caching, you need to employ in-memory cache or distributed caching.
  • Please see the description for Response Caching Middleware from the documentation page I referred to. It clearly states that it stores and serves responses. You might be confusing it with Response Caching.
  • This answer is incorrect. ResponseCaching in ASP.Net Core does both client-side caching (through headers) & Server-Side Caching (similar to OutputCache in previous versions of ASP.Net). OP was specifically asking why the server-side caching wasn't working.
  • @ChrisPratt I have read the source. I have also successfully used it. You may have been correct about an earlier version of .net core's ResponseCaching, but it has also been a server-side cache for over a year now.