有没有一种方法可以让我把一些代码放在我的页面上,这样当有人访问一个网站时,它就会清空浏览器缓存,这样他们就可以查看更改了?
使用语言:ASP。净,VB。NET,当然还有HTML, CSS和jQuery。
有没有一种方法可以让我把一些代码放在我的页面上,这样当有人访问一个网站时,它就会清空浏览器缓存,这样他们就可以查看更改了?
使用语言:ASP。净,VB。NET,当然还有HTML, CSS和jQuery。
当前回答
如果有兴趣,我已经找到了我的解决方案,让浏览器在。net MVC (.net fw 4.8)和使用包的情况下刷新.css和.js。 我想让浏览器仅在部署新程序集之后刷新缓存文件。
根据撒利亚多尼的回答,我的解决方案如下:
store your application base url in the web config app settings (the HttpContext is not yet available at runtime during the RegisterBundle...), then make this parameter changing according to the configuration (debug, staging, release...) by the xml transform In BundleConfig RegisterBundles get the assembly version by the means of reflection, and... ...change the default tag format of both styles and scripts so that the bundling system generates link and script tags appending a query string parameter on them.
下面是代码
public static void RegisterBundles(BundleCollection bundles)
{
string baseUrl = system.Configuration.ConfigurationManager.AppSettings["by.app.base.url"].ToString();
string assemblyVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString();
Styles.DefaultTagFormat = $"<link href='{baseUrl}{{0}}?v={assemblyVersion}' rel='stylesheet'/>";
Scripts.DefaultTagFormat = $"<script src='{baseUrl}{{0}}?v={assemblyVersion}'></script>";
}
你会得到这样的标签
<script src="https://example.org/myscriptfilepath/script.js?v={myassemblyversion}"></script>
您只需要记住在部署之前构建一个新版本。
Ciao
其他回答
这里是在ASP.NET中设置缓存的MDSN页面。
Response.Cache.SetExpires(DateTime.Now.AddSeconds(60))
Response.Cache.SetCacheability(HttpCacheability.Public)
Response.Cache.SetValidUntilExpires(False)
Response.Cache.VaryByParams("Category") = True
If Response.Cache.VaryByParams("Category") Then
'...
End If
您是想清除缓存,还是只是确保当前(已更改的?)页面没有缓存?
如果是后者,那就应该如此简单
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
并非如此。一种方法是在传递内容时发送适当的头文件,以迫使浏览器重新加载:
确保网页没有在所有浏览器上缓存。
如果你在SO上搜索“缓存头”或类似的东西,你会发现ASP。NET特定的示例。
另一种不太清晰但有时是唯一的方法,如果你不能控制服务器端的头,就是给被调用的资源添加一个随机的GET参数:
myimage.gif?random=1923849839
强制浏览器清除缓存或重新加载正确的数据?我已经尝试了stackoverflow中描述的大多数解决方案,一些工作,但过了一会儿,它最终缓存并显示先前加载的脚本或文件。是否有另一种方法,将清除缓存(css, js等),并实际工作在所有浏览器?
I found so far that specific resources can be reloaded individually if you change the date and time on your files on the server. "Clearing cache" is not as easy as it should be. Instead of clearing cache on my browsers, I realized that "touching" the server files cached will actually change the date and time of the source file cached on the server (Tested on Edge, Chrome and Firefox) and most browsers will automatically download the most current fresh copy of whats on your server (code, graphics any multimedia too). I suggest you just copy the most current scripts on the server and "do the touch thing" solution before your program runs, so it will change the date of all your problem files to a most current date and time, then it downloads a fresh copy to your browser:
<?php
touch('/www/sample/file1.css');
touch('/www/sample/file2.js');
?>
然后……剩下的程序…
It took me some time to resolve this issue (as many browsers act differently to different commands, but they all check time of files and compare to your downloaded copy in your browser, if different date and time, will do the refresh), If you can't go the supposed right way, there is always another usable and better solution to it. Best Regards and happy camping. By the way touch(); or alternatives work in many programming languages inclusive in javascript bash sh php and you can include or call them in html.
如果这是关于.css和.js的更改,那么一种方法是通过在每个版本的文件名后面附加类似“_versionNo”的东西来“缓存破坏”。例如:
script_1.0.css // This is the URL for release 1.0
script_1.1.css // This is the URL for release 1.1
script_1.2.css // etc.
或者在文件名后面:
script.css?v=1.0 // This is the URL for release 1.0
script.css?v=1.1 // This is the URL for release 1.1
script.css?v=1.2 // etc.
您可以检查这个链接,看看它是如何工作的。