ASP.NET MVC SiteMap provider -- How to 'hide' single items in the actual menu
Asked Answered
R

2

9

I am using the ASP.NET MVC SiteMap provider in a project, and it is working great. I am having a tough time trying to figure out how to hide a menu item however. The menu item I want to hide from displaying in the global navigation is my "Site Map" page. Now I know that there is something called a VisibilityProvider available to me on the mvcSiteMapNode - but I can't seem to figure out how to make it work.

Rhymester answered 17/8, 2012 at 13:47 Comment(0)
S
5

First, I suggest you read this wiki page: Creating a Custom SiteMapNodeVisibilityProvider. Then for the specific node that points to your Site Map page, declare it this way:

<mvcSiteMapNode title="Site Map" controller="Home" action="Map" visibility="false" />

Now, when implementing the IsVisible method (shown in the wiki page linked above), you can do this:

string visibility = mvcNode["visibility"];

// Is a visibility attribute specified?
if (!string.IsNullOrEmpty(visibility))
{
     isVisible = Convert.ToBoolean(mvcNode["visibility"]);

     if (!isVisible)
     {
          return false;
     }
}

return true;
Succession answered 23/9, 2012 at 10:48 Comment(0)
S
6

Taken from my answer here explaining how to hide nodes and options available.

https://mcmap.net/q/1315925/-mvc-sitemap-hiding-a-node-from-menuhelper-but-display-in-sitepathhelper-breadcrumbs

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

Sheronsherourd answered 23/11, 2014 at 23:34 Comment(1)
This is the easiest solution, because it does not require additional code.Unbeknown
S
5

First, I suggest you read this wiki page: Creating a Custom SiteMapNodeVisibilityProvider. Then for the specific node that points to your Site Map page, declare it this way:

<mvcSiteMapNode title="Site Map" controller="Home" action="Map" visibility="false" />

Now, when implementing the IsVisible method (shown in the wiki page linked above), you can do this:

string visibility = mvcNode["visibility"];

// Is a visibility attribute specified?
if (!string.IsNullOrEmpty(visibility))
{
     isVisible = Convert.ToBoolean(mvcNode["visibility"]);

     if (!isVisible)
     {
          return false;
     }
}

return true;
Succession answered 23/9, 2012 at 10:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.