Here's a little trick for any ASP.NET coders out there...
Problem: I've got a site where I'm always changing my CSS styles, and end-users weren't getting the changes I was making because their browser cached the CSS files (that's a good thing). The only option was to modify my master pages to point to a new file name, but that gets tedious when you're making almost daily changes.
Solution: I whipped up a bit of code that looks in the CSS folder on my site, finds the last saved file, and serves that to the user. If the filename hasn't changed, the browser will skip it and use the one stored in cache.
On my dev site I can open the current CSS file, make my changes, save as a different file name (I prefer YearMonthDayLetter.css), upload the new file to the production site, and the new file will get served without any code changes to my existing pages. Yay!
protected void Page_Load(object sender, EventArgs e)
{
LatestStyleSheet();
}
protected void LatestStyleSheet()
{
string cssPhysicalPath = System.IO.Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath, "css");
string[] fileList = Directory.GetFiles(cssPhysicalPath);
DateTime lastwritten = Convert.ToDateTime("1/1/2000");
string latestCSSfile = String.Empty;
foreach (string file in fileList)
{
FileInfo finfo = new FileInfo(file);
if (finfo.LastWriteTime > lastwritten)
{
lastwritten = finfo.LastWriteTime;
latestCSSfile = file.Substring(file.LastIndexOf("\\") + 1);
}
}
HtmlLink link = new HtmlLink();
link.Attributes.Add("type", "text/css");
link.Attributes.Add("rel", "stylesheet");
link.Attributes.Add("media", "screen");
link.Attributes.Add("href", "/css/" + latestCSSfile);
Page.Header.Controls.Add(link);
}