javascript - force browsers to get latest js and css files in asp.net application -
Some browsers cache JS and CSS files, until they force them to refresh, until you force them. What is the easiest way.
I have implemented this solution which seems to work.
Announce a version variable on your page
public string version {get; Set; }
Get the version number from the web. Config key
version = configuration manager. App settings ["version number"];
To make calls such as JavaScript and stylesheets in your ASPX page
& lt; Script src = "scripts / myjavascript.js? V = & lt;% = version%> & gt; type =" text / javascript "& gt; & Lt; / Script & gt; & Lt; Link href = "Styles / Mastile css? V = & lt;% = version%> & gt; Rel = "stylesheet" type = "text / css" />
So if you set 1.0 to 1.0 version in your web.config, then your browser will download the latest files, which is expected to save you and your users from some frustration.
Does any other solution work better, or will it cause any unexpected issues for the website?
I resolved it by attacking the last modified timestamp as a query parameter for scripts.
I did it with an extension method, and used it in my CSLLL files Note: This implementation caches the timestamp for 1 minute so we can convert the disc to Do not be too cool.
The extension method here is:
public static class JavaScript Extension {public static MvcHtmlString contains it VersionedJs (this HtmlHelper helper, string filename) {string version = GetVersion (Helpful, File name); Return MvcHtmlString.Create ("& lt; script type = 'text / javascript' src = '" + file name + version + "' '& gt; & lt; / script & gt;"); } Private Static String GetVersion (this HtmlHelper helper, string filename) {var reference = helpful. Viewfench File request HttpContext; If (context.Cache [filename] == null) {var physicalPath = context.Server.MapPath (filename); Var version = $ "? V = {new system.IO.FileInfo (physical path). Light WrightTime.ToString (" MMddHHmmss ")}"; Context.Cache.Add (file name, version, blank, date time.Now extra minutes (5), timespace.zoro, cache item. Normal, zero); Return version; } Else {return context.Cache [filename] as a string; }}}
and then in the CSHML page:
@html Include variance ("/ migration_files.js")
In the rendered HTML, it appears:
& lt; Script type = 'text / javascript' src = '/ myjavascriptfile.js? 20111129120000 '& gt; & Lt; / Script & gt;
Comments
Post a Comment