加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – IE 8和客户端缓存

发布时间:2020-12-16 04:04:09 所属栏目:asp.Net 来源:网络整理
导读:背景故事: 我在IIS 6 Web服务器上有一个.NET 3.5的Web门户.目前,有一个页面被赋予一个值,并且基于该值在Web服务上查找PDF文件,并在网页的另一个选项卡中将结果显示给用户.这是通过以下代码完成的. context.Response.ClearContent(); context.Response.Clear
背景故事:

我在IIS 6 Web服务器上有一个.NET 3.5的Web门户.目前,有一个页面被赋予一个值,并且基于该值在Web服务上查找PDF文件,并在网页的另一个选项卡中将结果显示给用户.这是通过以下代码完成的.

context.Response.ClearContent();
 context.Response.ClearHeaders();
 context.Response.Clear();
 context.Response.AddHeader("Accept-Header",pdfStream.Length.ToString());                                               
 context.Response.ContentType = "application/pdf";
 context.Response.BinaryWrite(pdfStream.ToArray());
 context.Response.Flush();

这工作并且已经工作了多年.但是,我们从客户端遇到一个问题,即特定客户每次都将PDF作为相同的PDF返回,直到他们清除临时Internet缓存.

我觉得很酷,这很容易.我只是将缓存标头添加到响应中,从不缓存它.所以我添加了以下内容:

context.Response.Cache.SetCacheability(HttpCacheability.NoCache);//IE set to not cache
context.Response.Cache.SetNoStore();//Firefox/Chrome not to cache
context.Response.Cache.SetExpires(DateTime.UtcNow); //for safe measure expire it immediately

经过快速测试后,我得到了我在响应标题中所期待的内容.

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

问题:

所以这就开始了.一天看起来很酷.第二天,bam,每个人都开始获得白色屏幕并且没有显示PDF.经过进一步调查,我发现它只是IE 6,7,8. Chrome很好,Firefox很好,Safari很好,甚至IE 9还不错.在不知道发生这种情况的原因的情况下,我恢复了我的改变并进行了部署,一切都恢复了.

我一直在搜索,试图找出为什么我的缓存标题似乎混淆IE 6-8无济于事. IE 6-8有没有人遇到过这类问题?有什么我想念的吗?感谢您的任何见解.

解决方法

我找到了解决方案.这是让我失望的原因. Here is a link

基本上IE8(及更低版本)如果没有缓存或存储缓存,则遇到Cache-Control标头问题.我能够通过基本上只允许私有缓存来解决问题,并将最大年龄设置为非常短,因此它几乎立即到期.

//Ie 8 and lower have an issue with the "Cache-Control no-cache" and "Cache-Control store-cache" headers.
//The work around is allowing private caching only but immediately expire it.
if ((Request.Browser.Browser.ToLower() == "ie") && (Request.Browser.MajorVersion < 9))
{
     context.Response.Cache.SetCacheability(HttpCacheability.Private);
     context.Response.Cache.SetMaxAge(TimeSpan.FromMilliseconds(1));
}
else
{
     context.Response.Cache.SetCacheability(HttpCacheability.NoCache);//IE set to not cache
     context.Response.Cache.SetNoStore();//Firefox/Chrome not to cache
     context.Response.Cache.SetExpires(DateTime.UtcNow); //for safe measure expire it immediately
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读