微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

asp.net-mvc – 如果ActionResult未更改,则将MVC.NET OutputCache配置为返回304 Not Modified

介绍问题

如果服务器指示304 Not Modified,我们已成功配置浏览器缓存以返回已保存的响应.这是配置:

<caching>
  <outputCacheSettings>
    <outputCacheProfiles>
      <add
        name="TransparentClient"
        location="Client"
        duration="0" />
    </outputCacheProfiles>
  </outputCacheSettings>
</caching>

web.config是完美的,并设置Cache-control:private,max-age = 0,以便:

>浏览器将缓存响应,
>将始终验证缓存,并且
>如果服务器响应304,将返回缓存的响应.

问题是我们的MVC.NET动作总是响应200而不是304.

问题

当ActionResult没有改变时,我们如何配置输出缓存以返回304 Not Modified?

> MVC.NET中是否有内置的缓存验证?
>如果不是,我们如何推出自己的缓存验证机制?

roll-our-own可能需要一个带ETag或Last-Modified的Action Filter.

屏幕截图

这是一个fiddler截图,显示缺少304.

> 318是SHIFT刷新.
> 332是一个我们预期会导致304的刷新.问题.

搜索和研究

ASP.NET MVC : how do I return 304 “Not Modified” status?提到从Action中返回304.这并没有提供使OutputCache准确响应304的方法.

Working with the Output Cache and other Action Filters显示了如何覆盖OnResultExecuted,这将允许添加/删除标头.

解决方法

以下内容适用于我们.

Web.Config

设置Cache-Control:private,max-age-0以启用缓存并强制重新验证.

<system.web>
  <caching>
    <outputCacheSettings>
      <outputCacheProfiles>
        <add name="TransparentClient" duration="0" location="Client" />
      </outputCacheProfiles>
    </outputCacheSettings>
  </caching>
</system.web>

行动

如果未修改响应,则回复304.

[MyOutputCache(CacheProfile="TransparentClient")]
public ActionResult ValidateMe()
{
    // check whether the response is modified
    // replace this with some ETag or Last-Modified comparison
    bool isModified = DateTime.Now.Second < 30;

    if (isModified)
    {
        return View();
    }
    else
    {
        return new HttpStatusCodeResult(304,"Not Modified");
    }
}

过滤

删除Cache-Control:private,max-age-0否则缓存将存储状态消息.

public class MyOutputCache : OutputCacheAttribute
{
    public override void OnResultExecuted(ResultExecutedContext filterContext)
    {
        base.OnResultExecuted(filterContext);
        if (filterContext.HttpContext.Response.StatusCode == 304)
        {
            // do not cache the 304 response                
            filterContext.HttpContext.Response.CacheControl = "";                
        }
    }
}

Fidder酒店

fiddler表明缓存行为正常.

原文地址:https://www.jb51.cc/aspnet/247180.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐