Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

asp.net core static file caching doesn't work

$
0
0

I am using the Angular project template with ASP.NET Core. Reference

The caching should be enabled by default, to be sure I have added an StaticFileOptions object to the StaticfileMiddleware:

public class Startup
{
 // ...
 app.UseStaticFiles(new StaticFileOptions
 {
     OnPrepareResponse = ctx =>
         {
             const int durationInSeconds = 60 * 60 * 24;
             ctx.Context.Response.Headers[HeaderNames.CacheControl] ="public,max-age=" + durationInSeconds;
         }
 });
 // ...
}

Then I have created an test.html in wwwroot folder.

<!DOCTYPE html><html lang="en"><head><meta charset=utf-8><title>Test static files</title></head><body>
    This is a test for static files!</body></html>

When I dotnet run and navigate to https://localhost:5001/test.html the page is displayed correctly.

test.html

After I change the html page, I click to google chrome's address bar and press enter, to issue another HTTP request to the test.html file. I have not reload the page.

test.html after change

You can see that the change is reflected by google chrome and I received an status 200 again instead of 304. Cache-control in the Response Header is public,max-age=86400. Why does the static file caching not work? My desired result would be that the change is not reflected after the second HTTP request, so I can be sure static file caching is working properly.

I am using: .NET Core SDK 3.1.201 and Chrome 80.0.3987.163.


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>