如何使用ASP.NET删除浏览器的缓存服务器端?

时间:2012-12-03 04:32:47

标签: c# asp.net

如何使用ASP.NET(C#)删除浏览器的缓存服务器端?

优惠券本身显示(我相信它来自缓存,因为我也浏览其他服装网站)。它打破了我的JavaScript以及我的服务器端代码,因为我使用的是Ajax的UpdatePanel,它复制了UpdatePanel的ID。我已经重命名了UpdatePanel的ID,但它没有任何区别。它会生成“视图状态无效”异常。优惠券名称为“FastSave”

我尝试过:

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

3 个答案:

答案 0 :(得分:2)

你可以像这样停止缓存:

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

答案 1 :(得分:0)

以下是我在ASP.NET MVC 2应用程序中设置客户端浏览器缓存的方法:

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

答案 2 :(得分:0)

你不能。

您可以告诉客户端不要缓存某些东西,但一旦它确实没有服务器端机制来清除缓存。您必须等待缓存过期或用户清除缓存。