title: ASP.Net Core 2.0 - ResponseCaching Middleware - Not Caching on Server
body: <p>I want to use server-side response caching (output cache) with asp.net core 2.0 and found out about <a href="https://docs.microsoft.com/en-us/aspnet/core/performance/caching/middleware?tabs=aspnetcore2x" rel="noreferrer">Response Caching Middleware</a> and wanted to give it a try with a brand new asp.core mvc project. </p>
  
  <p>Here is the description from the link above which makes me think this could be used like output cache.</p>
  
  <blockquote>
    <p>The middleware determines when responses are cacheable, stores responses, and serves responses from cache.</p>
  </blockquote>
  
  <p>Here is how my startup.cs looks like.</p>
  
  <pre><code>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)
      {
          services.AddResponseCaching();
          services.AddMvc();
      }
  
      // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
      public void Configure(IApplicationBuilder app, IHostingEnvironment env)
      {
          app.UseResponseCaching();
  
          if (env.IsDevelopment())
          {
              app.UseBrowserLink();
              app.UseDeveloperExceptionPage();
          }
          else
          {
              app.UseExceptionHandler("/Home/Error");
          }
  
          app.UseStaticFiles();
  
          app.UseMvc(routes =&gt;
          {
              routes.MapRoute(
                  name: "default",
                  template: "{controller=Home}/{action=Index}/{id?}");
          });
      }
  }
  </code></pre>
  
  <p>and here is the HomeController.cs</p>
  
  <pre><code>[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 });
      }
  }
  </code></pre>
  
  <p>there is also a timestamp at the bottom of _Layout.cshtml file so i can tell when the page is rendered, like below.</p>
  
  <pre><code>&lt;p&gt;&amp;copy; 2018 - ResponseCachingMiddleware - @DateTime.UtcNow&lt;/p&gt;
  </code></pre>
  
  <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.</p>
  
  <pre><code>Cache-Control:public,max-age=60
  </code></pre>
  
  <p>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 <a href="https://docs.microsoft.com/en-us/aspnet/core/performance/caching/response" rel="noreferrer">Response Caching</a> seems to be just a filter to manipulate response headers for caching. </p>
  
  <p>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.</p>
  
  <p>I've checked out this post before 
  <a href="https://stackoverflow.com/questions/46492736/asp-net-core-2-0-http-response-caching-middleware-nothing-cached">ASP.NET Core 2.0 - Http Response Caching Middleware - Nothing cached</a></p>
  
  <p>Also checked this out but it seems like the only difference is I'm using mvc.
  <a href="https://github.com/aspnet/ResponseCaching/blob/dev/samples/ResponseCachingSample/Startup.cs" rel="noreferrer">https://github.com/aspnet/ResponseCaching/blob/dev/samples/ResponseCachingSample/Startup.cs</a></p>
  
  <p>Thanks</p>
  
  <p>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.</p>
  
  <blockquote>
    <p>Microsoft.AspNetCore.ResponseCaching.ResponseCachingMiddleware:Information:
    The response could not be cached for this request.</p>
  </blockquote>
  
  <p>Note: I wish I could create #response-caching-middleware tag. Not sure #responsecache is relevant.</p>