Click here to Skip to main content
15,888,600 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
How to force clear cache in asp.net
Posted

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


Put this code in page load. Now there will be no data in cache browser will try to get the page from the serv
 
Share this answer
 
Comments
Member 13603097 1-Feb-18 8:07am    
its not working
i try this but it seems not working

public void DisablePageCaching()
{
//Used for disabling page caching
HttpContext.Current.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
HttpContext.Current.Response.Cache.SetValidUntilExpires(false);
HttpContext.Current.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.Cache.SetNoStore();
HttpContext.Current.Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1));
}

private void ClearCache()
{
DictionaryEntry entry = default(DictionaryEntry);
foreach (DictionaryEntry entry_loopVariable in System.Web.HttpContext.Current.Cache)
{
entry = entry_loopVariable;
System.Web.HttpContext.Current.Cache.Remove(entry.Key.ToString());
}

IDictionaryEnumerator enumerator = HttpContext.Current.Cache.GetEnumerator();

while (enumerator.MoveNext())
{
HttpContext.Current.Cache.Remove(enumerator.Key.ToString());
}
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.Expires = 0;
HttpContext.Current.Response.CacheControl = "no-cache";
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache);
HttpContext.Current.Response.Cache.SetNoStore();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.ExpiresAbsolute =DateTime.Now.Subtract(new TimeSpan(1,0,0,0));
HttpContext.Current.Response.AppendHeader("Pragma", "no-cache");
HttpContext.Current.Response.AppendHeader("", "");
HttpContext.Current.Response.AppendHeader("Cache-Control", "no-cache"); //HTTP 1.1
HttpContext.Current.Response.AppendHeader("Cache-Control", "private"); // HTTP 1.1
HttpContext.Current.Response.AppendHeader("Cache-Control", "no-store"); // HTTP 1.1
HttpContext.Current.Response.AppendHeader("Cache-Control", "must-revalidate"); // HTTP 1.1
HttpContext.Current.Response.AppendHeader("Cache-Control", "max-stale=0"); // HTTP 1.1
HttpContext.Current.Response.AppendHeader("Cache-Control", "post-check=0"); // HTTP 1.1
HttpContext.Current.Response.AppendHeader("Cache-Control", "pre-check=0"); // HTTP 1.1
HttpContext.Current.Response.AppendHeader("Pragma", "no-cache"); // HTTP 1.1
HttpContext.Current.Response.AppendHeader("Keep-Alive", "timeout=3, max=993"); // HTTP 1.1
}
 
Share this answer
 
Comments
Anuj Banka 19-Dec-11 6:22am    
You Have to use the code of lines that i have given on page load of every page.what these code of lines will do if you are on page load of one page it will delete the cache data of the previous page.
If you want set clear cache of current page then write below code on button click as well as on page load, It will work for me.

HttpResponse.RemoveOutputCacheItem("/pagecontentdetail.aspx")
 
Share this answer
 
Comments
Dave Kreskowiak 17-Jul-14 13:03pm    
Why are you responding to 3 year old questions?? DON'T!
copa017 26-Sep-19 7:50am    
Why not responding to 3 year old question Dave???
People like me come here by googling this problem (it is one of the 1st search results), so it is very useful to have updated answers - and they are not only for the T.O..
Dave Kreskowiak 26-Sep-19 8:12am    
Because what is appropriate for one version of ASP.NET may not be appropriate for newer versions.

Also, this answer doesn't give you any control over the caching policy. Instead, it's a "one shot kill" for the entire page. What's more common is to cache the page, but not certain items on the page, like data grids.
<script type="text/javascript">
<!--
function reloadIt() {
var clocktime = new Date();
var utchours = clocktime.getUTCHours();
var utcminutes = clocktime.getUTCMinutes();
var utcseconds = clocktime.getUTCSeconds();
var utcyear = clocktime.getUTCFullYear();
var utcmonth = clocktime.getUTCMonth()+1;
var utcday = clocktime.getUTCDate();

if (utchours <10) { utchours = "0" + utchours }
if (utcminutes <10) { utcminutes = "0" + utcminutes }
if (utcseconds <10) { utcseconds = "0" + utcseconds }
if (utcmonth <10) { utcmonth = "0" + utcmonth }
if (utcday <10) { utcday = "0" + utcday }

var utctime = utcyear + utcmonth + utcday;
utctime += utchours + utcminutes + utcseconds;
x = utctime

isNew = self.location.href
if(!isNew.match('#','x')) {
self.location.replace(isNew + '#' + x)
}
}

//-->
</script>

</head>
<body onLoad="reloadIt()">
 
Share this answer
 
Try:

1. Set cache
C#
Cache[ "username" ] = "username";
Cache[ "password" ] = "password";


2. Use cache
C#
if( Cache[ "username" ] != null && Cache[ "password" ] != null )
{
   //cache is still alive
   String username = Cache[ "username" ].ToString();
   String password= Cache[ "password" ].ToString();
}
else
{
   //show error, cache objects do not exist!
}


3. Clear cache
C#
Cache[ "username" ] = null;
Cache[ "password" ] = null;


Happy coding,
Morgs
 
Share this answer
 
Try:

1. Set cache
Cache[ "username" ] = "username";
Cache[ "password" ] = "password";

2. Use cache
if( Cache[ "username" ] != null && Cache[ "password" ] != null )
{
//cache is still alive
String username = Cache[ "username" ].ToString();
String password= Cache[ "password" ].ToString();
}
else
{
//show error, cache objects do not exist!
}

3. Clear cache
Cache.Remove("username");
Cache.Remove("password");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900