Instantly share code, notes, and snippets.

Embed
What would you like to do?
An ASP.NET MVC HTML helper method that renders a menu from a SiteMap
public static string Menu(this HtmlHelper helper, string siteMapProviderName)
{
var provider = SiteMap.Providers[siteMapProviderName];
return NodesAsHtml(provider.RootNode);
}
private static string NodesAsHtml(SiteMapNode node)
{
var writer = new StringWriter();
if (node.HasChildNodes)
{
if (!String.IsNullOrEmpty(node.Title))
{
writer.Write("<span>{0}</span>", node.Title);
}
writer.Write("<ul>");
foreach (SiteMapNode childNode in node.ChildNodes)
{
writer.Write("<li>");
writer.Write(NodesAsHtml(childNode));
writer.Write("</li>");
}
writer.Write("</ul>");
}
else
{
writer.Write("<a href=\"{0}\">{1}</a>", node.Url, node.Title);
}
return writer.ToString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment