MVC SiteMap Hiding a node from menuhelper, but dis

2020-07-18 06:03发布

问题:

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.

回答1:

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



回答2:

You should use this guide on how to hide a node

https://github.com/maartenba/MvcSiteMapProvider/wiki/Advanced-Node-Visibility-with-ISiteMapNodeVisibilityProvider

Some settings you can set from the link above:

<appSettings>
    <!-- Visibility will not filter to children -->
    <add key="MvcSiteMapProvider_VisibilityAffectsDescendants" value="false"/>
    <!-- Set default visibility provider -->
    <add key="MvcSiteMapProvider_DefaultSiteMapNodeVisibiltyProvider" value="MvcSiteMapProvider.FilteredSiteMapNodeVisibilityProvider, MvcSiteMapProvider"/>
</appSettings>

Once you have added the app settings, add the following to any node you want to see in the breadcrumbs but not the menu:

visibility="SiteMapPathHelper,!*" (SiteMapPathHelper - the node is visible in the sitemappath, !* - it is invisible for all other controls)

eg:

<mvcSiteMapNode title="Administration" area="Admin" clickable="false" visibility="SiteMapPathHelper,!*" />

Other options available:

Type..........................What it Affects
CanonicalHelper.......The Canonical HTML Helper
MenuHelper..............The Menu HTML Helper
MetaRobotsHelper....The Meta Robots HTML Helper
SiteMapHelper..........The SiteMap HTML Helper
SiteMapPathHelper...The SiteMapPath HTML Helper
SiteMapTitleHelper...The Title HTML Helper
XmlSiteMapResult....The sitemaps XML output of the /sitemap.xml endpoint



回答3:

add this to your web.config

<appSettings>
  <add key="MvcSiteMapProvider_DefaultSiteMapNodeVisibiltyProvider" value="MvcSiteMapProvider.FilteredSiteMapNodeVisibilityProvider, MvcSiteMapProvider"/>
<appSettings>