IE 8和客户端缓存

时间:2022-05-15 15:45:29

Background story:

I have a web portal in .NET 3.5 on an IIS 6 web server. Currently there is a page that is given a value and based on that value looks up a PDF file on a web service and displays the results to the user in another tab in the web page. This is done with the following code.

我在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();

This works and has worked for years. However we got an issue from the client that a particular client was having the PDF returned as the same PDF every time until they cleared temp internet cache.

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

I thought oh cool, this is an easy one. I will just add the cache headers to the response to never cache it. So I added the following:

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

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 

After a quick test I got exactly what I was expecting in the response header.

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

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

The Problem:

So this went live. Everything seemed cool day one. The day after, bam, everyone started getting white screens and no PDF displayed. After further investigation, I found out it was only IE 6,7,8. Chrome is fine, Firefox fine, safari fine, even IE 9 fine. Without knowing the why this happened, I reverted my change and deployed it, and everything started worked again.

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

I have searched all over trying to find out why my caching headers seemed to confuse IE 6-8 to no avail. Has anyone experienced this type of issue with IE 6-8? Is there something I am missing? Thanks for any insight.

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

1 个解决方案

#1


6  

I found the solution. Here is what tipped me off. Here is a link

我找到了解决方案。这是让我失望的原因。这是一个链接

Basically IE8 (and lower) was having issues with the Cache-Control header if it had no-cache or store-cache. I was able to work around the problem by basically allowing private caching only and set a max age to very short so it expires almost immediately.

基本上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
}

#1


6  

I found the solution. Here is what tipped me off. Here is a link

我找到了解决方案。这是让我失望的原因。这是一个链接

Basically IE8 (and lower) was having issues with the Cache-Control header if it had no-cache or store-cache. I was able to work around the problem by basically allowing private caching only and set a max age to very short so it expires almost immediately.

基本上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
}