Search code examples
asp.net-mvcsitemapproviderasp.net-mvc-sitemap

ASP MVC Sitemap is there an Image HTML Helper?


I have an ASP MVC Sitemap which look something like this

<mvcSiteMapNode title="Home" imageUrl="home.png" controller="Home" action="Index">
        <mvcSiteMapNode title="Search" controller="Search" imageUrl="magnifying_glass.png" action="Index">

The nodes all have an 'imageUrl' property assigned to them which I would like to access within my view. I know there is a SiteMap helper included within the library which allows me to the get the title via

@Html.MvcSiteMap().SiteMapTitle()

However, I can't see any way of obtaining the imgUrl. Before I write my own, does anybody know if this already exists? I've search around but can't find any way of doing it within the existing library.


Solution

  • OK, I've just written something myself. It's very simple.

    public static class MvcSiteMapImageUrlHelper
        {
            public static MvcHtmlString SiteMapImageUrl(this MvcSiteMapHtmlHelper helper)
            {
                var node = helper.Provider.CurrentNode;
    
                if (node != null)
                {
                    return new MvcHtmlString(node["ImageUrl"]);
                }
    
                return new MvcHtmlString(string.Empty);
            }
        }