Saturday, January 30, 2010

Ultra-Fast ASP.NET

Ultra-Fast ASP.NET

Just started reading Ultra-Fast ASP.NET by Richard Kiessig (Build Ultra-Fast and Ultra-Scalable web sites using ASP.NET and SQL Server).

So far very good. Will post more here.

Richard Kiessig on twitter:


Saturday, January 23, 2010

Automatically keeping CSS file current on any web page

The problem: You update your site's CSS file but your users aren't seeing your latest crazy colors and styles that you've selected for your web site.

The reason: Your users' browsers have cached the CSS files and it could take days before those caches expire and your new CSS file is requested from your server.

The solution: Change the name of your CSS file that's linked in the header of your page each time you change the contents of the CSS file.

The new problem: You don't want to change the name manually each time because (1) it may need changing in more than one place, (2) you might forget to change it, (3) you're lazy, (4) you might miss somewhere it needs to be changed, (5) each change increases the risk you might do something wrong.

The new solution: Have it done automatically for you.

This is how I implemented it using ASP.NET and C#. I did this on a hybrid ASP.NET MVC and WebForms web site that has two base master pages; one for MVC and one for WebForms. All other master pages inherit from these two master pages so there were just two locations that need to be changed.

In the <head> tag I originally had something like this:

<link href="/site.css" rel="stylesheet" type="text/css" media="all" />

and I wanted something like this:

<link href="/site.css?v=1" rel="stylesheet" type="text/css" media="all" />

with the 1 changing each time the site.css file changed.

In any major project that I'm working on I usually have a class called something like StaticData. This class holds arbitrary bits of data that are loaded or calculated once and then never or rarely change. It's like a hybrid of a constants file and a cache.

In this class I added the following property and private variable:

string _CssVersion = null;
public string CssVersion
        if (String.IsNullOrEmpty(_CssVersion))
                string cssFile = System.Web.HttpContext.Current.Server.MapPath("~/site.css");
                FileInfo fi = new FileInfo(cssFile);
                DateTime lastWriteTime = fi.LastWriteTime;
                _CssVersion = lastWriteTime.ToString("yyyyMMddHHmmss");
                return "1";
        return _CssVersion;

So what I'm doing is generating a version number based on the time stamp of the CSS file. If we update the CSS file then that time stamp will automatically change and we'll only have to load it once because after that it's in the "constants cache."

To load the CSS name dynamically in ASP.NET we add the following code snippet to the Page_Load() function of the code behind file of the master page. This applies to both WebForms and MVC applications.

HtmlLink css = new HtmlLink();
css.Href = String.Format("/site.css?v={0}", StaticData.Instance.CssVersion);
css.Attributes["rel"] = "stylesheet";
css.Attributes["type"] = "text/css";
css.Attributes["media"] = "all";

The Instance property of the StaticData class is a public static property of type StaticData making this class a singleton.

Tuesday, January 19, 2010

Graffiti CMS now open source

This blog runs on Graffiti CMS. I have wined in the past about this not being open source and have on ocassion thought about moving it to an open source blog engine. Today I read that Graffiti CMS is now open source. I have never done a blow by blow comparison of .NET blog engines so I don't know if Graffiti is the best but I can say that it has worked very smoothly for me and has some clever built in features which I like.

This is a great contribution to the open source community by Telligent - thank you.

Source code is available here:

Monday, January 18, 2010

jQuery drag and drop tree plugin

I've been researching a jQuery drag and drop tree plugin for a project that I'm working on and so far I've found the following:

1. jsTree

So far this is my favorite. It does almost everything that I want including in place editing of the tree items. One drawback is that it itself requires a ton of plugins to work and is complex to setup and this makes it brittle in my opinion. The creator, however, is frantically working on a new release which I think will simplify things and dramatically improve this already excellent plugin. I've decided that this is the one that I'll probably use but will wait until the next major release comes out.

Fantastic set of demo pages - this is what "sells" plugins - if you don't have a create demo page then you will dramatically reduce your chances of getting users to use your plugin.

2. SimpleTree

Good but does not support in place editing. Big plus that it has a demo page but the demo is fairly simple and I suspect that it has more features that have not been demo'd.

3. Drag Drop Tree

The demo looks reasonable but no work has been done on this (so it seems) since 2007 and there's not much other documentation about this plugin.


Friday, January 8, 2010

Null Coalescing Assignment Operator for C#

Great post by Chris Eargle about a null coalescing assignment operator for c#.

How would you like to be able to say:

myObj ??= new MyObj();

instead of:

myObj = myObj ?? new MyObj();

Seems like a trivial "enhancement" to the language but makes sense to me.

Monday, January 4, 2010

Flags enum attribute

I've just learned about the [Flags] attribute for enums. I'm surprised that I hadn't stumbled across it before. Consider the following code snippet:

enum Direction { North = 1, East = 2, South = 4, West = 8 }
static void EnumTesting()
    Direction ne = Direction.North | Direction.East;

The output is: 3

Now add the [Flags] attribute to the enum:

enum Direction { North = 1, East = 2, South = 4, West = 8 }
static void EnumTesting()
    Direction ne = Direction.North | Direction.East;

The output is now: North, East

Enumeration constants are generally used for lists of mutually exclusive elements. However, sometimes you may want to use them for lists of elements that might occur in combination. Permissions to access resources is a good example of this.