Reputation: 39
I am wondering if it's possible to enable automatic clearing of cache in browser when the application is accessed? The thing is that sometimes when the user accessed the newly published asp.net mvc site in their browser they need to clear their browser cache to see the new site since the previous version of the site is the one that reflects in their browser. I have no idea about the right approach to do this. Can someone please shed some light? Thank you a bunch !
Upvotes: 1
Views: 3657
Reputation: 5677
The right approach is to have Cache Busting implemented in your website.
Cache busting is the process of appending some form of file version hash to the filename of resources like JavaScript and CSS files
It will automatically take care of cache to get performance (if file is not changed) and will not allow the cache if file is changed.
If you use bundling and minification in asp.net ,then also this will be taken care for you.
asp.net bundling creates url contains a query string pair like v=r0sLDicvP58AIXN_mc3QdyVvVj5euZNzdsa2N1PKvb81
The query string v has a value token that is a unique identifier used for caching. As long as the bundle doesn't change, the ASP.NET application will request the AllMyScripts bundle using this token. If any file in the bundle changes, the ASP.NET optimization framework will generate a new token, guaranteeing that browser requests for the bundle will get the latest bundle
Upvotes: 0
Reputation: 155
Have you tried
// Stop Caching in IE
Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
// Stop Caching in Firefox
Response.Cache.SetNoStore();
Upvotes: 0
Reputation: 2525
when you say clear cache I am assuming you mean js
and css
files
If so, one way to do it is when need refresh, give it a different url
This can be achieve by appending a version number in querystring such as:
my.js?v=1.0
so when it is updated to v2 change it to
my.js?v=2.0
and my.js will be updated
same for css file
Upvotes: 1