2011-05-12 94 views
2

我正在开发一个动态Web应用程序(在IIS7上运行),它在所有主流浏览器(除IE9以外)都能正常工作。看来,这缓存几乎一切,这导致了相当多的问题,如IE9缓存动态页面

  • 经常变化的内容保持不变
  • 用户访问授权的内容,然后登出,然后试图回到安全内容并从缓存中获取!

我试图禁用缓存,

<meta http-equiv="Expires" CONTENT="0"> 
<meta http-equiv="Cache-Control" CONTENT="no-cache"> 
<meta http-equiv="Pragma" CONTENT="no-cache"> 

但至今没有运气...

回答

1

你是否大量使用AJAX?确保每个AJAX请求都是唯一的,否则IE9将提供缓存版本的请求响应。

例如,如果您的AJAX请求URL通常是这样的: http://www.mysite.com/ajax.php?species=dog&name=fido

相反,一个独特的价值添加到每个请求,所以IE不只是使用缓存的响应。使用Javascript最简单的方法是每次提出请求时都会增加的变量:

var request_id = 0; 

var request_url = "http://www.mysite.com/ajax.php?species=dog&name=fido&request_id="+request_id; 
request_id++; 
+1

如果这是问题,并且他们使用jQuery for AJAX,这是一个很好的全局解决方案:http://www.peteonsoftware.com/index.php/2010/08/20/the-importance- of-jquery-ajaxsetup-cache/ – 2011-05-12 12:56:40

+0

如果你使用jquery,那么这很有用,但不是每个人都这样做(我知道我不知道)。 – 2011-05-12 12:58:01

+0

这是有帮助的:http://stackoverflow.com/questions/367786/prevent-caching-of-ajax-call – BeaverProj 2011-07-06 22:24:09

3

我刚刚在MVC开发中遇到过这个问题。

我想禁用所有AJAX请求缓存服务器端。

为此,我注册了以下全局过滤器。

public class AjaxCacheControlAttribute: ActionFilterAttribute 
{ 
    public override void OnResultExecuted(ResultExecutedContext filterContext) 
    { 
     if (filterContext.RequestContext.HttpContext.Request.IsAjaxRequest()) 
     { 
      filterContext.HttpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1)); 
      filterContext.HttpContext.Response.Cache.SetValidUntilExpires(false); 
      filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache); 
      filterContext.HttpContext.Response.Cache.SetNoStore(); 
     } 
    } 
}