dfm*_*tro 6 asp.net-core-mvc asp.net-core
间歇性代理导致我的页面被我部署的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();
});
Run Code Online (Sandbox Code Playgroud)
我可以在fiddler中看到,我的页面以及javascript文件,css文件和图像文件中都没有添加缓存标头.
我如何限制这个没有缓存标题只适用于asp.net mvc页面所以这些没有缓存标题不会出现在fiddler中的非页面文件,如js,css和图像文件
有没有办法让HTTP请求css和js文件不检查服务器上是否存在每个请求的文件,而只是浏览器版本用于第一次获取这些文件.我问的原因是,在重负载(1000个用户)我在Fiddler我发现我为我的css,js和图像文件得到了HTTPGET的404错误,所以我试图限制对这些资源的请求数量.当请求成功(没有加载)时,我得到304个响应(未修改).是否有办法告诉浏览器不首先发出请求并使用本地缓存版本.
Jor*_*ion 14
app.UseStaticFiles(new StaticFileOptions()
{
OnPrepareResponse =
r =>
{
string path = r.File.PhysicalPath;
if (path.EndsWith(".css") || path.EndsWith(".js") || path.EndsWith(".gif") || path.EndsWith(".jpg") || path.EndsWith(".png") || path.EndsWith(".svg"))
{
TimeSpan maxAge = new TimeSpan(7, 0, 0, 0);
r.Context.Response.Headers.Append("Cache-Control", "max-age=" + maxAge.TotalSeconds.ToString("0"));
}
}
});
Run Code Online (Sandbox Code Playgroud)
这是一种应该适合您的方法。此示例将 css 和 js 文件设置为由浏览器缓存 7 天,并将非 css、js 和图像设置为没有缓存标头。另请注意,只需在 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();
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3617 次 |
| 最近记录: |