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);
}

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
I decided it was time for me to learn a new language. Para el español, presione el número dos. Just kidding, I'm talking about a new programming language. Seeing how I'm a Classic ASP ninja, I figure the .NET framework should suit me fine. They're both based on VBScript, right? Not.

The code that ASP.NET 1.1 spits out is awful. The lack of modern web standards support has been talked about in detail, and some have done a great job at cleaning it up. I want to learn it, but how can someone that believes in web standards honestly use it without feeling dirty? Enter ASP.NET 2.0. It seems the Visual Studio team is doing wonders with their application development software, especially their new (and temporarily free) Visual Web Developer 2005 (beta). If you haven't heard about it, I suggest you check it out.

They've got a lot of good selling points, but here's what sold me on making the switch:
  • Defaults new pages to XHTML 1.1. And the code validates. Almost.
  • Switching to Design View won't mess with my code/styles.
  • .NET 1 came out in 2000. A lot's changed since then. The MS guys seem to have noticed what developers want. It's about time...
I've noticed a few things that bug me. But before I say anything, I'm going to wait until Beta 2 comes out. In the mean-time, I feel less-dirty about learning ASP.NET, because I know 2.0 is right around the corner.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5