4
votes

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
Could you please tell me which cached content come back? You have use the memory cache or else in your asp.net core application? If possible, please update the related codes. - Brando Zhang
Although I was using some in memory caching (prior to this post) I've since removed it. I'm not aware of any other explicit caching being performed in my code. The content that is being cached is basic HTML content being returned from a database - Note that I am not using any query caching. I am using EF Core as an ORM but again, I'm not using any explicit caching there. I'm wondering if there is some implicit caching happening somewhere... - Jason Webb
Do you mean you find the MVC home index action's response has been cached in your browser? - Brando Zhang
It is unclear to me what exactly may be getting cached as everything I understand about caching suggests that the content being displayed on the page should not be getting cached. However, the result is that the content being displayed from the response appears to be getting cached somehow. Whether it is the response, or the browser, is unclear to me. Might be time for a proof of concept around caching :( - Jason Webb

1 Answers

0
votes

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.