I'm relatively new to ASP.NET Core 3 and am building a simple proof of concept around a CMS. I am attempting to demonstrate that we are able to cache static content (.jpg, .png, etc.) but prevent caching for dynamic (content delivered from database into the model). The static content seems to be working fine. The dyncamic content, however, is caching even though the Response Headers appears to be set properly.
My Response Headers as seen from Microsoft Edge Developer Tools are:
cache-control: no-cache, no-store, must-revalidate
content-type: text/html; charset=utf-8
date: Mon, 29 Jun 2020 18:25:58 GMT
expires: 0
pragma: no-cache
server: Kestrel
status: 200
At this point, I am not using
app.UseResponseCaching();
I am only using this for static files within the Startup.cs
app.UseStaticFiles(new StaticFileOptions // for wwwroot files
{
OnPrepareResponse = (context) =>
{
var headers = context.Context.Response.GetTypedHeaders();
headers.CacheControl = new CacheControlHeaderValue
{
Public = true,
MaxAge = TimeSpan.FromDays(30)
};
}
});
and this
services.AddMvc(options =>
{
options.Conventions.Add(new RouteTokenTransformerConvention(
new SlugifyParameterTransformer()));
options.CacheProfiles.Add("Default0", new CacheProfile()
{
NoStore = true,
Location = ResponseCacheLocation.None,
Duration = 0
});
})
...Added this to my Controller class
[ResponseCache(CacheProfileName = "Default0")]
public class CmsController : CmsControllerBase<CmsPageModel>
...And for good measure, I've also placed this directly in my View:
@using Microsoft.Net.Http.Headers;
@using Microsoft.AspNetCore.Http;
@model CMS.Web.Models.CmsPageModel
@{
this.Context.Response.Headers[HeaderNames.CacheControl] = "no-cache, no-store, must-revalidate";
this.Context.Response.Headers[HeaderNames.Expires] = "0";
this.Context.Response.Headers[HeaderNames.Pragma] = "no-cache";
}
I've hit F5 on the browser several times and no matter what I do, the same cached content comes back until I rebuild the app and re-launch it. Any ideas programmer friends?