6

I'm a newbie web developer, and ASP.NET Core is the first web framework I've worked with. I noticed that my site wasn't updating when I hit F5, and I tracked it down to the browser cache being enabled. If I open Chrome DevTools > Network and select 'Disable brower cache', my site gets updated properly.

However, I don't want to go through this process every time I start debugging. Is there a way to disable caching programmatically in my ASP.NET Core app, e.g. through the Startup.cs file, so that this will automatically be done for me?

edit: I am using UseFileServer instead of UseStaticFiles. (I am not sure what either does, but UseFileServer works correctly for debugging while UseStaticFiles does not.) I tried passing a FileServerOptions argument to UseFileServer, but that doesn't let you configure the cache expiry like StaticFilesOptions does.

If it's relevant, I'm building a Web API project.

3
  • @I.Manev See my updated description.
    – James Ko
    Jan 20, 2018 at 19:41
  • What about you disable caching on your browser instead. Jan 20, 2018 at 20:08
  • Browsers have a few key commands to bypass caching like ctl+f5
    – Tratcher
    Jan 20, 2018 at 22:16

1 Answer 1

5

You can still use StaticFileOptions when using UseFileServer:

var fsOptions = new FileServerOptions();
fsOptions.StaticFileOptions.OnPrepareResponse = (context) =>
{
    // Disable caching of all static files.
    context.Context.Response.Headers["Cache-Control"] = "no-cache, no-store";
    context.Context.Response.Headers["Pragma"] = "no-cache";
    context.Context.Response.Headers["Expires"] = "-1";
}
app.UseFileServer(fsOptions);
2
  • This is not correct. The StaticFilesOptions is a readonly property. Please edit your answer to be like this: var fsOptions = new FileServerOptions(); fsOptions.StaticFileOptions.OnPrepareResponse = ...; app.UseFileServer(fsOptions);. That's what worked for me.
    – James Ko
    Jan 20, 2018 at 23:39
  • Thanks for update. That will teach me to work without an IDE.
    – Pace
    Jan 20, 2018 at 23:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.