admin管理员组文章数量:1312795
I want to clear the browser cache in MVC application.
I added the below code on my .cshtml page and its working for IE and Firefox.
Response.ExpiresAbsolute = DateTime.Now;
Response.Expires = 0;
Response.CacheControl = "no-cache";
Response.Buffer = true;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.UtcNow);
Response.Cache.SetNoStore();
Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
I am looking for a solution which will work on chrome as well.
I want to clear the browser cache in MVC application.
I added the below code on my .cshtml page and its working for IE and Firefox.
Response.ExpiresAbsolute = DateTime.Now;
Response.Expires = 0;
Response.CacheControl = "no-cache";
Response.Buffer = true;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.UtcNow);
Response.Cache.SetNoStore();
Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
I am looking for a solution which will work on chrome as well.
Share Improve this question asked Apr 19, 2013 at 9:23 SanoopSanoop 1191 gold badge2 silver badges11 bronze badges 2- Are the requests being cached ajax requests? – Beez Commented Apr 23, 2013 at 12:48
- The above solution did NOT work for me in IE. – Obi Wan Commented Oct 15, 2013 at 13:33
2 Answers
Reset to default 4Use the Following Attribute:
public class NoCacheAttribute : ActionFilterAttribute
{
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
if (filterContext == null) throw new ArgumentNullException("filterContext");
var cache = GetCache(filterContext);
cache.SetExpires(DateTime.UtcNow.AddDays(-1));
cache.SetValidUntilExpires(false);
cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
cache.SetCacheability(HttpCacheability.NoCache);
cache.SetNoStore();
base.OnResultExecuting(filterContext);
}
/// <summary>
/// Get the reponse cache
/// </summary>
/// <param name="filterContext"></param>
/// <returns></returns>
protected virtual HttpCachePolicyBase GetCache(ResultExecutingContext filterContext)
{
return filterContext.HttpContext.Response.Cache;
}
}
}
Simply add this to your base controller :
[NoCache]
public BaseController: Controller
This worked for me when cachebusting ajax requests on Chrome.
Response.AddHeader("Cache-Control", "no-cache, no-store, max-age=0, must-revalidate");
Response.AddHeader("Expires", "Fri, 01 Jan 1990 00:00:00 GMT");
Response.AddHeader("Pragma", "no-cache");
本文标签: javascriptHow to clear browser cache in MVC applicationStack Overflow
版权声明:本文标题:javascript - How to clear browser cache in MVC application? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741902497a2403947.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论