tag:blogger.com,1999:blog-125341290284114978.post737013655831526295..comments2023-04-23T00:05:10.829-07:00Comments on Guy Ellis' Tech Blog: Automatically keeping CSS file current on any web pageGuy Ellishttp://www.blogger.com/profile/02574435376236977220noreply@blogger.comBlogger5125tag:blogger.com,1999:blog-125341290284114978.post-32384258044229359452012-11-09T12:04:30.000-08:002012-11-09T12:04:30.000-08:00This is terrific, over two years later and I was a...This is terrific, over two years later and I was able to adapt this for a razor website, for CSS and JS links. Thanks!Jasonnoreply@blogger.comtag:blogger.com,1999:blog-125341290284114978.post-59253041298751582252010-03-19T10:34:19.000-07:002010-03-19T10:34:19.000-07:00Sam - you are correct about the caching of the CSS...Sam - you are correct about the caching of the CSS file inasmuch as only a restart will force a read on the value. This is designed for the CSS to be changed to be changed at each deployment which goes along with a reset so that is why this paradigm works in this situation. However, if you're deploying CSS files without an application reset then you would have to implement it the way that you described.guy ellisnoreply@blogger.comtag:blogger.com,1999:blog-125341290284114978.post-58358739595580522692010-03-19T07:30:46.000-07:002010-03-19T07:30:46.000-07:00Hmm, I see you check if there is a value in cssver...Hmm, I see you check if there is a value in cssversion:<br />if (String.IsNullOrEmpty(_CssVersion))<br />But I don't see a dependency on the file. How will changing the contents of the file trigger the version to be reloaded? Once you have set the version you are always returning it regardless of a file change. Only a restart will force you to read the value again?<br />I may be missing something.<br />I have updated the CssVersion property to be as follows:<br />public static string CssVersion<br /> {<br /> get<br /> {<br /> if (HttpContext.Current.Cache["CssVersion"] == null)<br /> {<br /> FileInfo style = new FileInfo(HttpContext.Current.Server.MapPath("~/styles/site.css"));<br /> HttpContext.Current.Cache.Insert("CssVersion", <br /> style.LastWriteTime.ToString("yyyyMMddHHmmss"), <br /> new CacheDependency(style.FullName), <br /> DateTime.Now.AddMinutes(60), <br /> TimeSpan.Zero,<br /> CacheItemPriority.Default,<br /> null);<br /> }<br /> return HttpContext.Current.Cache["CssVersion"].ToString();<br /> }<br /> }<br />This will trigger the version to be reloaded should be file be changed.Samnoreply@blogger.comtag:blogger.com,1999:blog-125341290284114978.post-92005666212123648282010-01-23T19:30:25.000-08:002010-01-23T19:30:25.000-08:00I was thinking of using a version number but the C...I was thinking of using a version number but the CSS file doesn't change with each version number so I wanted to keep the file's signature the same and only change it if the CSS file changed. That's why I resorted to the date/time stamp for the version just for the CSS file.guy ellisnoreply@blogger.comtag:blogger.com,1999:blog-125341290284114978.post-3433692738237165042010-01-23T16:10:14.000-08:002010-01-23T16:10:14.000-08:00I've done something similar except I tie it to...I've done something similar except I tie it to the current version of the page's assembly via reflection. Thus whenever there's a new build, the version number automatically changes. (It's great for local box development also.)Bill Brownnoreply@blogger.com