I am using package Microsoft.AspNet.StaticFiles and configuring it in Startup.cs as app.UseStaticFiles(). How can I change the headers
If you're looking for a solution allowing you to configure different behaviours for each environment (development, production & more), which is also the point of having these settings in the web.config file instead of hard-coding the whole stuff, you could consider the following approach.
Add the following key/value section in the appsettings.json file:
"StaticFiles": {
"Headers": {
"Cache-Control": "no-cache, no-store",
"Pragma": "no-cache",
"Expires": "-1"
}
}
Then add the following in the Startup.cs file's Configure method accordingly:
app.UseStaticFiles(new StaticFileOptions()
{
OnPrepareResponse = (context) =>
{
// Disable caching for all static files.
context.Context.Response.Headers["Cache-Control"] = Configuration["StaticFiles:Headers:Cache-Control"];
context.Context.Response.Headers["Pragma"] = Configuration["StaticFiles:Headers:Pragma"];
context.Context.Response.Headers["Expires"] = Configuration["StaticFiles:Headers:Expires"];
}
});
This will allow the developer to define different cache settings using different/multiple/cascading settings files (appsettings.json, appsettings.production.json and so on) - which is something that could be done with the old web.config configuration pattern - with the ASP.NET Core's new one.
For additional info regarding the topic I also suggest to read this post on my blog and/or these great articles from the official ASP.NET Core docs: