简体   繁体   中英

How can I remove a browser's cache server-side, using ASP.NET?

How can I remove a browser's cache server-side, using ASP.NET (C#)?

A coupon shows by itself (I believe it comes from cache as I did also browse for other apparel sites). It breaks my JavaScript as well as my server-side code, as I am using an UpdatePanel for Ajax, and it duplicates the UpdatePanel's ID. I have renamed the UpdatePanel's ID, but it makes no difference. It generates "Invalid view State" exception. The coupon name is "FastSave"

What I have tried:

Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();

you can stop cache like this:

protected void Page_Load(object sender, EventArgs e)
{
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.Cache.SetExpires(DateTime.Now);
    Response.Cache.SetNoServerCaching();
    Response.Cache.SetNoStore();
}

Here's how I set client-side browser caching in my ASP.NET MVC 2 app:

public class CacheFilterAttribute : ActionFilterAttribute {

    /// <summary>
    /// Gets or sets the cache duration in seconds. The default is 10 seconds.
    /// </summary>
    /// <value>The cache duration in seconds.</value>

    public int Duration { get; set; }

    public CacheFilterAttribute() { Duration = 10; }

    public override void OnActionExecuted(ActionExecutedContext filterContext) {
        if (Duration <= 0) return;

        var cache = filterContext.HttpContext.Response.Cache;
        var cacheDuration = TimeSpan.FromSeconds(Duration);

        cache.SetCacheability(HttpCacheability.Public);
        cache.SetExpires(DateTime.Now.Add(cacheDuration));
        cache.SetMaxAge(cacheDuration);
        cache.AppendCacheExtension("must-revalidate, proxy-revalidate");
    }
}

You can't.

You can tell the client not to cache something, but once it does there is no server side mechanism to clear the cache. You have to wait for the cache to expire or the user to clear the cache.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM