我正在做一个asp.net MVC web应用程序和我需要知道在为我的操作方法定义OutputCache时是否有任何差异,如下所示:-

[OutputCache(Duration = 0, Location = OutputCacheLocation.Client, VaryByParam = "*")]

VS

[OutputCache(NoStore = true, Duration = 0, Location="None", VaryByParam = "*")]

VS

[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]

以上三个设置是否都会阻止缓存数据,或者每个设置都有不同的含义?

Second question定义duration=0duration=0之间的主要区别是什么;NoStore=true ? 它们都会阻止缓存吗?

推荐答案

NoStore属性用于通知代理服务器和浏览器,它们不应通过在请求头中设置Cache-Control: no-store来存储缓存内容的永久副本.

Duration simply specifies how long the content of the controller action should be cached, e.g. 10seconds. This will set the Cache-Control: max-age to >= 0. And also sets the Expires header to a valid timestamp.

对于你最初的问题,不,这三种变体的含义不同.

[OutputCache(Duration = 0, Location = OutputCacheLocation.Client, VaryByParam = "*")]

创建这样的缓存头

Cache-Control: private, max-age=0
Expires: Fri, 03 Jan 2014 12:32:15 GMT

[OutputCache(NoStore = true, Duration = 0, Location="None", VaryByParam = "*")]

创建以下缓存标头:

Cache-Control: no-cache, no-store
Pragma: no-cache
Expires: -1

这基本上就是你想要看到的,如果你想通过各种方式阻止缓存.VaryByParam是可选的(至少在MVC5中是这样),默认值是"*",因此您可以简单地使用[OutputCache(NoStore = true, Location = OutputCacheLocation.None)].


[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]

甚至创建了一个公共缓存控件...

Cache-Control: public, no-store, max-age=0
Expires: Fri, 03 Jan 2014 12:36:38 GMT

在SO上有一个很好的帖子,讨论了max-age=0 and no-cache etc.max-age=0 and no-cache etc.之间的区别.

最后,这三种方法可能会阻止缓存数据,但仍有不同的含义.

Asp.net相关问答推荐

log4net 记录所有未处理的应用程序错误

在正则表达式中允许 -(破折号)

在 ASP.NET 中访问没有值的查询字符串参数

asp.net、url 重写模块和 web.config

威胁已经被清除了

在控制器 asp.net mvc 5 的视图上显示错误消息

在属性中实现逻辑是一种好习惯吗

.NET 核心 IdentityUser 模型中规范化邮箱和用户名的用途是什么?

忽略默认文档的表单身份验证

Jquery asp.net 按钮单击事件通过 ajax

IIS 7.5 上没有 asp.net 功能

显示单选按钮列表内联

ASP.Net 自定义客户端验证

这个rendersection的代码是什么意思?

@(at) 登录文件路径/字符串

如何在 ASP.NET 中获取原始请求正文?

Web API 必填参数

会话存储在客户端还是服务器端

使用 ASP.NET 进行重定向后获取

将对象传递给 HTML 属性