【问题标题】:ASP.NET FileDependency caching not working when accessing the page访问页面时 ASP.NET FileDependency 缓存不起作用
【发布时间】:2013-07-15 23:40:17
【问题描述】:

基本上我想做的是一个完整的服务器端缓存,并禁用客户端缓存。

[OutputCache(Duration = 3600)]
public ActionResult getImage(string imagePath)
{
    Response.AddFileDependency(imagePath);
    return base.File(imagePath, "image/jpg");
}

“刷新”页面时,缓存可以完美运行。 返回“status: 304 Not-Modified”,手动修改文件时切换到“status: 200”。

但是,当通过超链接(RedirectToAction)从另一个页面访问来呈现页面时,或者通过单击 url 栏并按 Enter 来呈现页面时,即使文件已更改,它也会返回“状态:200(缓存)”。

我尝试了以下组合

  • Response.Cache.SetETagFromFileDependencies();
  • Response.Cache.SetLastModifiedFromFileDependencies();
  • Response.Cache.SetCacheability(HttpCacheability.Server);
  • Response.Cache.VaryByHeaders["If-None-Match"] = true;

但没有任何效果。

如果有人能引导我找到解决方案,那就太好了。

【问题讨论】:

    标签: caching asp.net-mvc-4


    【解决方案1】:

    我解决了!

    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();
    }
    

    使用的来源 http://bloggingabout.net/blogs/ramon/archive/2011/06/16/mvc-always-returns-status-code-200-when-a-max-age-is-set-while-it-can-return-with-304.aspx

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-01
      • 2018-02-21
      • 1970-01-01
      • 1970-01-01
      • 2016-12-16
      • 2017-09-20
      • 2015-10-19
      相关资源
      最近更新 更多