How can I programmatically clear cache?

偶尔善良 提交于 2019-11-28 11:35:56

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

You can remove a page from the output cache as follows:

HttpResponse.RemoveOutputCacheItem("MyPage.aspx");

This won't remove it from any client-side cache, so if you want to use this technique you will probably want to disable client-side cache, e.g. by using the following directive in your aspx page:

<%@ OutputCache Location="Server" ...

Unless there's some javascript way to clear the cache (which would be awful), you can't.

Your best bet is to ensure the page doesn't get cached at all, by doing as Sukhi suggests - or setting up a no-cache cache profile and using the OutputCache directive.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!