Programmatically add Meta Tags to ASP.NET Master Pages using a SiteMap

I hadn’t really used SiteMaps before, but it’s a useful feature of ASP.NET 2.0 (most people using them to drive BreadCrumb controls and site navigation).

After realising there was no support for design of the sitemap (apart from the default text editor) I found a simple (and functional) SiteMap editor here.

I then realised that I needed to add meta tags for each page in the site (NOTE: this is a simple ‘content’ site, so don’t come running to me if you have the need to generate specific tags based on what colour trousers the user is wearing!).

You can add new attributes very easily to the sitemap, and access them programmatically.  The editor has a nice little grid to make this extra easy for you.  In my example here we’ve added a ‘keywords’ attribute. 

Assuming you’re using a treeview (or something similar) for navigation on your site and you’re binding to the SiteMap data source, then you’ll get the context of the current site map node in your page (or master page).

You can then use it to set your meta tags as follows:

You can also set Page.Title from currNode.Title etc.

This is actually quite a neat way to drive things like other standard properties of your master page – e.g. subheadings.