间歇性代理导致我的页面被我部署的 asp.net 核心站点缓存。 Web 服务器没有缓存页面。
我添加了请求和响应缓存以防止此代理导致的任何缓存
在我的 Startup.cs
app.UseStaticFiles();
app.UseIdentity();
app.Use(async (context, next) =>
{
context.Response.Headers.Append("Cache-Control", "no-cache");
context.Response.Headers.Append("Cache-Control", "private, no-store");
context.Request.Headers.Append("Cache-Control", "no-cache");
context.Request.Headers.Append("Cache-Control", "private, no-store");
await next();
});
我可以在 fiddler 中看到这些没有缓存头被添加到我的页面以及 javascript 文件、css 文件和图像文件中。
最佳答案
这是一种应该适合您的方法。本示例将 css 和 js 文件设置为浏览器缓存 7 天,并将非 css、js 和图像设置为没有缓存头。另一个注意事项是,只需要在响应上设置缓存控制 header 是 Asp.NET Core。
app.Use(async (context, next) =>
{
string path = context.Request.Path;
if(path.EndsWith(".css") || path.EndsWith(".js")) {
//Set css and js files to be cached for 7 days
TimeSpan maxAge = new TimeSpan(7, 0, 0, 0); //7 days
context.Response.Headers.Append("Cache-Control", "max-age="+ maxAge.TotalSeconds.ToString("0"));
} else if(path.EndsWith(".gif") || path.EndsWith(".jpg") || path.EndsWith(".png")) {
//custom headers for images goes here if needed
} else {
//Request for views fall here.
context.Response.Headers.Append("Cache-Control", "no-cache");
context.Response.Headers.Append("Cache-Control", "private, no-store");
}
await next();
});
关于asp.net-core - 如何将 css、js 或图像文件缓存到 asp.net 核心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37706370/