-
Notifications
You must be signed in to change notification settings - Fork 218
Advanced Node Visibility
In some situations, nodes should be visible in the breadcrumb trail but not in a complete sitemap. This can be solved using the concept of ISiteMapNodeVisibilityProvider, that can be specified globally for every node in the sitemap or granularly on a specific sitemap node.
It is possible to implement your own ISiteMapNodeVisibilityProvider, as is done in the sample application. There is also a standard FilteredSiteMapNodeVisibilityProvider, which filters sitemap node visibility based on the control that is currently rendering the sitemap node.
In order to make use of FilteredSiteMapNodeVisibilityProvider, the following steps should be taken:
1 - Modify Web.config and set the attributesToIgnore attribute to contain "visibility", like so:
<siteMap defaultProvider="MvcSiteMapProvider" enabled="true">
<providers>
<clear />
<add name="MvcSiteMapProvider"
type="MvcSiteMapProvider.DefaultSiteMapProvider, MvcSiteMapProvider"
...
attributesToIgnore="visibility"
...
/>
</providers>
</siteMap>
2 - If the *FilteredSiteMapNodeVisibilityProvider* should be used globally for all nodes in the sitemap, modify Web.config and set the siteMapNodeVisibilityProvider attribute to "MvcSiteMapProvider.FilteredSiteMapNodeVisibilityProvider, MvcSiteMapProvider". Here's an example: ```xml ```
3 - For every node, specify the visibility attribute. Here's an example: ```xml ```
The visibility attribute can contain a comma-separated list of controls in which the sitemap node should be rendered or not. These are processed left-to-right and can be inverted using an exclamation mark. Here's a break down of the above example:
Directive | Meaning |
---|---|
SiteMapPathHelper | The node is visible in the SiteMapPathHelper. |
!* | The node is invisible in any other control. |
Note that an implicit * (The node is visible in any control) is added at the end of the list.
In order to create a custom SiteMapNodeVisibilityProvider, to following steps should be taken:
1 - Create a new class implementing ISiteMapNodeVisibilityProvider
using System.Collections.Generic;
using System.Web;
using MvcSiteMapProvider;
using MvcSiteMapProvider.Extensibility;
namespace MyCompany
{
public class MyCustomVisibilityProvider : ISiteMapNodeVisibilityProvider
{
public bool IsVisible(SiteMapNode node, HttpContext context, IDictionary<string, object> sourceMetadata)
{
// Convert to MvcSiteMapNode
var mvcNode = node as MvcSiteMapNode;
if (mvcNode == null)
{
return true;
}
// Is a visibility attribute specified?
string visibility = mvcNode["visibility"];
if (string.IsNullOrEmpty(visibility))
{
return true;
}
visibility = visibility.Trim();
//process visibility
switch (visibility)
{
case "Condition1":
//...
return false;
case "Condition2":
//...
return false;
}
return true;
}
}
}
2 - Modify Web.config and set the attributesToIgnore attribute to contain "visibility" and the siteMapNodeVisibilityProvider to contain your Visibility Provider namespace and assembly like so:
<siteMap defaultProvider="MvcSiteMapProvider" enabled="true">
<providers>
<clear />
<add name="MvcSiteMapProvider"
type="MvcSiteMapProvider.DefaultSiteMapProvider, MvcSiteMapProvider"
...
siteMapNodeVisibilityProvider="MyCompany.MyCustomVisibilityProvider, MyWebProject"
attributesToIgnore="visibility"
...
/>
</providers>
</siteMap>
MyWebProject should be the name of the dll that is created by your MVC project
3 - For every node, specify the visibility attribute. Here's an example:
<mvcSiteMapNode title="Administration" area="Admin" visibility="Condition1" />
<mvcSiteMapNode title="Settings" area="Admin" visibility="Condition2" />
Want to contribute? See our Contributing to MvcSiteMapProvider guide.
- Registering the provider
- Creating a first sitemap
- Defining sitemap nodes in code
- Dynamic sitemaps
- HtmlHelper functions
- Action Filter Attributes
- Exporting the sitemap for search engine indexing
- Advanced node visibility
- Extension points
Other places around the web have some documentation that is helpful for getting started and finding answers that are not found here.
- MvcSiteMapProvider 4.0 - A Test Drive
- MvcSiteMapProvider 4.0 - SEO Features Tutorial
- How to Make MvcSiteMapProvider Remember a User’s Position
- MvcSiteMapProvider 4.0 - Cache Configuration
- MvcSiteMapProvider 4.0 - Extending the Cache
- MvcSiteMapProvider 4.0 - Unit Testing with the SiteMaps Static Methods
- Debugging an MvcSiteMapProvider Configuration
- Converting from C# to Vb MvcSiteMapProvider
- ASP.NET MVC Menu using Site Map Provider & Bootstrap 3 Navbar
- ASP.NET MVC SiteMapPath using Site Map Provider & Bootstrap Breadcrumbs
- NightOwl888's MvcSiteMapProvider Demos - Filter for "MvcSiteMapProvider" to see the most relevant.
- MvcSiteMapProvider Tutorial and Examples
- MvcSiteMapProvider Tutorial 2 - Breadcrumbs
- Getting Started with MvcSiteMapProvider
- Inside the MvcSiteMapProvider - Part 1
- Inside the MvcSiteMapProvider - Part 2: Dynamic node providers
- Inside the MvcSiteMapProvider - Part 3: The ISiteMapVisibilityProvider
- Inside the MvcSiteMapProvider - Part 4: The IAclModule
- Inside the MvcSiteMapProvider - Part 5: The ISiteMapNodeUrlResolver
- Styling MvcSiteMapProvider with CSS
- Using MvcSiteMapProvider with Twitter Bootstrap
- ASP.NET MVC Menu using Site Map Provider & Bootstrap Navbar