asp.net 清除客户端缓存
这几天在做菜单,使用iframe来切换页面,发现了个问题,当iframe的src指向.aspx文件时,页面总是会闪动一下,就像刷新了一样,但实际没有刷新,很是烦人,在网上也没找到问题所在,因为我同事也是这样做的,但他没出现这种问题,所以很奇怪,于是就对着代码对比,我一开始以为是布局的问题,后来发现不是,然后就对后台代码进行检查,结果发现清除掉客户端缓存后,切换页面时闪动的问题解决了,哈哈。也许碰到这种问题的人并不多,我记下来作为经验总结吧。
代码如下:
1 /// <summary>
2 /// 清除客户端缓存
3 /// </summary>
4 public static void ClearClientPageCache()
5 {
6 HttpContext.Current.Response.Buffer = true;
7 HttpContext.Current.Response.Expires = 0;
8 HttpContext.Current.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
9 HttpContext.Current.Response.AddHeader("pragma", "no-cache");
10 HttpContext.Current.Response.AddHeader("cache-control", "private");
11 HttpContext.Current.Response.CacheControl = "no-cache";
12 }
2 /// 清除客户端缓存
3 /// </summary>
4 public static void ClearClientPageCache()
5 {
6 HttpContext.Current.Response.Buffer = true;
7 HttpContext.Current.Response.Expires = 0;
8 HttpContext.Current.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
9 HttpContext.Current.Response.AddHeader("pragma", "no-cache");
10 HttpContext.Current.Response.AddHeader("cache-control", "private");
11 HttpContext.Current.Response.CacheControl = "no-cache";
12 }