日期:2014-05-16  浏览次数:20417 次

浏览器的缓存问题
    
    我在开发中遇到了这么一个问题:

      存在一个web页面,在这个页面内有一个信息列表,用户可以删除某个信息。

      当用户删除a信息的时候,采用的是异步请求,即:使用ajax技术向后台发送删除信息,后台删除成功,前台使用js删除a信息的html内容,完成删除功能。这时,用户点击页面内某个链接,去了其它页面,当用户按浏览器的后退按钮,再返回到信息也的时候,显然,浏览器提取的是缓存的html页面,显示a信息还未删除。
      
      请教各位,这个问题怎么解决?谢谢!
------解决方案--------------------
我写了一个NoCache的Attribute,你可以复制一下拿去用:

    public class NoCacheAttribute : ActionFilterAttribute
    {
        public override void OnResultExecuting(ResultExecutingContext filterContext)
        {
            filterContext.HttpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
            filterContext.HttpContext.Response.Cache.SetValidUntilExpires(false);
            filterContext.HttpContext.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
            filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            filterContext.HttpContext.Response.Cache.SetNoStore();
            base.OnResultExecuting(filterContext);
        }
    }

用法:

[NoCache]
public ActionResult Index(){
....
return View();
}
------解决方案--------------------
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="Expires" content="0">
<title>


    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = 0;
        Response.CacheControl = "no-cache";