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

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

背景故事:

我在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
}

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

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

相关推荐