Initialize Page Titles from Site Map
Posted in Development
The first time I saw a site map in ASP.NET 2.0, I thought, I wonder if page titles can be synchronized with corresponding nodes automatically. Even if it’s not possible "out-of-the-box", it’s easy enough to amend.
Firs off, why bother? Page titles and <Hx> elements should be meaningful. For SEO purposes I always try to keep the page title and the <H1> in sync.
To avoid going to every page and setting its Page.Title property, I’d rather have my code detect if there’s a node configured, get its title and description and initialize my page header accordingly.
A simple base class like this should do the job:
using System.Web.UI; using System.Web; public class PageBase : Page { protected override void OnInit (System.EventArgs e) { base.OnInit (e); SiteMapNode node = SiteMap.CurrentNode; if (node != null && !string.IsNullOrEmpty (node.Title)) { Page.Title = node.Title + " | " + node.Description; } } }
Now, there has always been enough arguing which separator character sounds better in a screen reader, so you decide whether it’s a pipe; a less-than or a greater-then sign; a colon, or what have you.
The downside of this approach is that you have to inherit your pages from the above base class. If you already have a base class, you’re almost set. Otherwise it can get tedious to replace base classes everywhere.
Common base class?
I thought I could set the pageBaseType attribute in the <pages> config section, but it has a very limited application: Specifies the base to use when the pages are stand-alone only.
Page adapter?
My next thought was to build a page adapter and have it take over an event or two in the event food chain. But once I realized I’d have to go to a .browser file and configure it with an adapter, my enthusiasm faded. Generally, I avoid adaptive rendering and device filtering. That’s one battle you can’t win. Win completely, that is.
2 comments
JasonM
on February 25, 2007
I saw Kent's comment - I didnt realise it was possible to add Custom properties to the sitemap !
Taking this a step further is it possible to merge the Google sitemap format with the ASP.NET sitemap ?

Kent Boogaart
on April 30, 2006
I did this recently myself, but I did it inside my master page. And I used the same approach to set keywords:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
if (SiteMap.CurrentNode != null)
{
Page.Header.Title = string.Format("MyWebSite : {0}", SiteMap.CurrentNode.Title);
//keywords taken from site map file
if (SiteMap.CurrentNode[_keywords] != null)
{
HtmlMeta keywordsMeta = new HtmlMeta();
keywordsMeta.Name = _keywords;
keywordsMeta.Content = SiteMap.CurrentNode[_keywords];
Page.Header.Controls.Add(keywordsMeta);
}
}
}
Another approach you could take is to implement a module and hook into the PreRequestHandlerExecute event:
void context_PreRequestHandlerExecute(object sender, EventArgs e)
{
Page page = HttpContext.Current.CurrentHandler as Page;
if (page != null)
{
page.PreInit += new EventHandler(page_PreInit);
}
}
void page_PreInit(object sender, EventArgs e)
{
Page page = sender as Page;
if (page != null)
{
//set the title and whatever else here
}
}
Cheers,
Kent