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

前端之家收集整理的这篇文章主要介绍了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.f2er.com/aspnet/247098.html

猜你在找的asp.Net相关文章