Search code examples
c#asp.net-mvcbreadcrumbsmvcsitemapprovidersitemapprovider

MVC SiteMap Hiding a node from menuhelper, but display in sitepathhelper (breadcrumbs)


I'm trying to hide a node from my site menu, but display it in my breadcrumbs

I'm following the tutorial here: https://github.com/maartenba/MvcSiteMapProvider/wiki/Advanced-Node-Visibility

<mvcSiteMapNode title="Create Customer" controller="Customer" action="Create" area="Home" clickable="false" visibility="SiteMapPathHelper,!*"/>  

The above doesn't seem to work. It shows up both in my site menu, and breadcrumbs.


Solution

  • We created an OnlyBreadCrumbMVCSiteMapNodeAttribute. We decorate any code we want the attribute

    public class OnlyBreadCrumbMvcSiteMapNodeAttribute : MvcSiteMapNodeAttribute
    {
        public OnlyBreadCrumbMvcSiteMapNodeAttribute(string title, string parentKey)
        {
            Title = title;
            ParentKey = parentKey;
            VisibilityProvider = typeof(BreadCrumbOnlyVisibilityProvider).AssemblyQualifiedName;
        }
        public OnlyBreadCrumbMvcSiteMapNodeAttribute(string title, string parentKey, string key)
        {
            Title = title;
            Key = key;
            ParentKey = parentKey;
            VisibilityProvider = typeof(BreadCrumbOnlyVisibilityProvider).AssemblyQualifiedName;
        }
    }
    

    Also have a visibilty provider

    public class BreadCrumbOnlyVisibilityProvider : ISiteMapNodeVisibilityProvider
    {
        public bool IsVisible(SiteMapNode node, HttpContext context, IDictionary<string, object> sourceMetadata)
        {
            if (sourceMetadata["HtmlHelper"] == null || (string)sourceMetadata["HtmlHelper"] == "MvcSiteMapProvider.Web.Html.SiteMapPathHelper")
            {
                return true;
            }
            return false;
        }
    }
    

    Use like

        [OnlyBreadCrumbMvcSiteMapNode("Upload Documents", "AssetDocuments")]
        public virtual ActionResult FileUpload(int assetId)
    

    Upload Documents will be breadcrumb title. AssetDocuments is the Parent Key

    If you pass the 3rd parameter, that sets a key of the breadcrumb node itself