Caching 访问页面时ASP.NET文件依赖项缓存不工作
基本上,我想做的是一个完整的服务器端缓存,并禁用客户端缓存Caching 访问页面时ASP.NET文件依赖项缓存不工作,caching,asp.net-mvc-4,Caching,Asp.net Mvc 4,基本上,我想做的是一个完整的服务器端缓存,并禁用客户端缓存 [OutputCache(Duration = 3600)] public ActionResult getImage(string imagePath) { Response.AddFileDependency(imagePath); return base.File(imagePath, "image/jpg"); } 当“刷新”页面时,缓存工作正常。 手动更改文件时,返回“状态:304未修改”,并切换到“状态:2
[OutputCache(Duration = 3600)]
public ActionResult getImage(string imagePath)
{
Response.AddFileDependency(imagePath);
return base.File(imagePath, "image/jpg");
}
当“刷新”页面时,缓存工作正常。
手动更改文件时,返回“状态:304未修改”,并切换到“状态:200”
但是,当通过超链接(RedirectToAction)从另一个页面访问页面或通过单击url栏并按enter键呈现页面时,即使文件已更改,也会返回“状态:200(缓存)”
我尝试了两种方法的组合
- Response.Cache.setGetFromFileDependencies()李>
- Response.Cache.SetLastModifiedFromFileDependencies()李>
- Response.Cache.SetCacheability(HttpCacheability.Server)李>
- Response.Cache.VaryByHeaders[“如果没有匹配”]=true李>
public ActionResult getImage(string imagePath)
{
return EtagFix(System.IO.File.GetLastWriteTime(imagePath).ToString(), HttpCacheability.Public, TimeSpan.FromHours(0)) ?? base.File(imagePath, "image/jpg");
}
ActionResult EtagFix(string etagResponse, HttpCacheability cachability, TimeSpan maxAge)
{
var cache = Response.Cache;
cache.SetETag(etagResponse);
cache.SetMaxAge(maxAge);
cache.SetCacheability(HttpCacheability.Public);
if (!etagResponse.Equals(Request.Headers["If-None-Match"])) return null;
Response.StatusCode = 304;
Response.StatusDescription = "Not Modified";
return new EmptyResult();
}
利用源