Search code examples
c#asp.netcache-control

How can I programmatically clear cache?


In my application (ASP.NET + c#) I need to clear the cache before a user enters an aspx page.

Does anybody have any idea how I can programmatically clear the cache on an aspx page, or in the code behind (c#)?


Solution

  • Write following code in the page load event:

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