SiteMapTitleAncestry missing from newest (2.2.1) release

Nov 8, 2010 at 4:09 PM

I noticed that SiteMapTitleAncestry was missing when I upgraded from 2.1. This HTML Helper was handy, so I added it back in, with a tweak, to allow reversing the order (prepending parent nodes instead of appending them)

/// <summary>
/// Gets the title of SiteMap.CurrentNode and appends all ancestor titles
/// </summary>
/// <param name="helper">MvcSiteMapHtmlHelper instance</param>
/// <param name="separator">Separator, i.e. " - "</param>
/// <returns>Title of SiteMap.CurrentNode and all ancestor titles</returns>
public static string SiteMapTitleAncestry(this MvcSiteMapHtmlHelper helper, string separator)
{
	return SiteMapTitleAncestry(helper, separator, false);
}

/// <summary>
/// Gets the title of SiteMap.CurrentNode and appends all ancestor titles
/// </summary>
/// <param name="helper">MvcSiteMapHtmlHelper instance</param>
/// <param name="separator">Separator, i.e. " - "</param>
/// <param name="reverse">Reverse the order (i.e. prepend instead of append)</param>
/// <returns>Title of SiteMap.CurrentNode and all ancestor titles</returns>
public static string SiteMapTitleAncestry(this MvcSiteMapHtmlHelper helper, string separator, bool reverse)
{
	var node = SiteMap.CurrentNode;
	if (node != null)
	{
		var titles = new List<string>();
		titles.Add(node.Title);
		while (node.ParentNode != null)
		{
			node = node.ParentNode;
			titles.Add(node.Title);
		}
		if (reverse) titles.Reverse();
		return string.Join(separator, titles.ToArray());
	}
	return "";
}

Maybe this will be of use to other people, or may come back in a future release.

Nov 10, 2010 at 6:52 AM

You can just use the SiteMapPath as well, only thing to do is create a custom template that reverses the nodes.