不想让客户端缓存(cache)

来源:互联网 发布:c语言指针教程 编辑:程序博客网 时间:2024/05/06 18:31

如果你不想让客户端缓存(cache)你服务器上的网页的话,在ASP.NET中可以这样控制:

在Page_Load中写:

Response.Cache.SetCacheability(HttpCacheability.NoCache);

我在测试的时候发现一些有趣的东西:

按HTTP 1.0的标准,你可以在html网页上直接使用 <meta http-equiv="pragma" content="no-cache" />

按HTTP 1.1的标准,你可以用:<meta http-equiv="Cache-Control" content="no-cache" />

为了保险起见当然是两个都用上。

但当我尝试在aspx页中加入这两个meta tag的时候,却看见返回的Response的Http Header里面居然显示Cache-Control: Private。

而且ASP.NET也没有办法通过程序来给Response写入pragma这个Http Header。

最后的方案是在aspx页中加入<meta http-equiv="pragma" content="no-cache" /> ,然后在程序中写Response.Cache.SetCacheability(HttpCacheability.NoCache);

这样,返回的Response里就有我预期的那两个header了:

Pragma: No-Cache

Cache-Control: No-Cache