简体   繁体   English

自动清除浏览器中的缓存

[英]Automatically clear cache in browser

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. 事实是,有时当用户在浏览器中访问新发布的asp.net mvc网站时,他们需要清除浏览器缓存才能看到新网站,因为该网站的上一版本是反映在其浏览器中的网站。 I have no idea about the right approach to do this. 我不知道执行此操作的正确方法。 Can someone please shed some light? 有人可以说明一下吗? Thank you a bunch ! 谢谢你一堆!

when you say clear cache I am assuming you mean js and css files 当您说清除缓存时,我假设您的意思是jscss文件

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 因此,当更新到v2时,将其更改为

my.js?v=2.0

and my.js will be updated 和my.js将被更新

same for css file 相同的CSS文件

Have you tried 你有没有尝试过

 // Stop Caching in IE
Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);

// Stop Caching in Firefox
Response.Cache.SetNoStore();

This might help you 这可能对您有帮助

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 缓存清除是将某种形式的文件版本哈希附加到JavaScript和CSS文件等资源的文件名中的过程

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中使用捆绑和缩小 ,那么这也会为您提供帮助。

asp.net bundling creates url contains a query string pair like v=r0sLDicvP58AIXN_mc3QdyVvVj5euZNzdsa2N1PKvb81 asp.net捆绑创建的URL包含一个查询字符串对,例如v=r0sLDicvP58AIXN_mc3QdyVvVj5euZNzdsa2N1PKvb81

The query string v has a value token that is a unique identifier used for caching. 查询字符串v具有值令牌,该值令牌是用于缓存的唯一标识符。 As long as the bundle doesn't change, the ASP.NET application will request the AllMyScripts bundle using this token. 只要捆绑包不变,ASP.NET应用程序就会使用此令牌请求AllMyScripts捆绑包。 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 如果捆绑软件中的任何文件发生更改,则ASP.NET优化框架将生成一个新令牌,以确保浏览器对捆绑软件的请求将获得最新的捆绑软件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM