asp.net core 3 Response Caching Not Working As Expected

Viewed 920

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:

enter image description here 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?

1 Answers

I never was able to figure this one out explicitly, but the solution came in the form of deleting all the files from the directory that the code was running from and then rebuilding the entire solution. It looks like there may have been some old cache related files abandoned by the solution but apparently still in a position to influence the application.

Related