【问题标题】:IHttp Generic Handler (.ashx) for Images cache validator用于图像缓存验证器的 IHttp 通用处理程序 (.ashx)
【发布时间】:2013-07-23 13:01:58
【问题描述】:

我有用于图像的 IHttp 通用处理程序 (.ashx),我已发送缓存过期时间为 7 天,但在 Google 页面洞察力中它说我应该添加缓存验证器。 以下是我的代码。请给我一些参考。

    public void ProcessRequest(HttpContext context)
    {
        TimeSpan refresh = TimeSpan.FromDays(7);
        context.Response.Cache.SetExpires(DateTime.Now.Add(refresh));
        context.Response.Cache.SetMaxAge(refresh);
        context.Response.Cache.SetCacheability(HttpCacheability.Public);
        context.Response.CacheControl = HttpCacheability.Public.ToString();
        context.Response.Cache.SetValidUntilExpires(true);
    }

谢谢

【问题讨论】:

    标签: asp.net-mvc-3 caching httpcontext ihttphandler generic-handler


    【解决方案1】:

    经过一番研究,我发现我们可以添加 Etag 或 SetLastModified 或者只是将标头添加到缓存中,如下所示:

    context.Response.Cache.SetETag(DateTime.Now.ToLongDateString());
    context.Response.Cache.SetLastModified(DateTime.Now.ToLongDateString());
    context.Response.AddHeader("Last-Modified", DateTime.Now.ToLongDateString());
    

    【讨论】:

      猜你喜欢
      • 2011-11-16
      • 1970-01-01
      • 2018-04-16
      • 2010-12-12
      • 2012-02-02
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      • 2013-03-16
      相关资源
      最近更新 更多