5

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.

4

2 回答 2

4

使用以下属性:

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;
    }
 }

}

只需将其添加到您的基本控制器:

[NoCache]
public BaseController: Controller
于 2013-04-28T11:55:14.707 回答
0

在 Chrome 上缓存 ajax 请求时,这对我有用。

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");
于 2013-04-28T12:19:48.270 回答