-
Notifications
You must be signed in to change notification settings - Fork 218
Defining Sitemaps using ISiteMapBuilder
Note: The technique described on this page can only be done using an external DI container.
While using IDynamicNodeProvider provides the ability to provide nodes based on database-driven or other dynamic data, it requires you to use either an XML file or .NET attributes, neither of which are ideal for multi-tenant applications. Also, IDynamicNodeProvider doesn't give you much control over the node hierarchy.
To enable more advanced scenarios, we created another interface, MvcSiteMapProvider.Builder.ISiteMapBuilder which has a single method, BuildSiteMap().
using System;
using MvcSiteMapProvider;
namespace MvcSiteMapProvider.Builder
{
/// <summary>
/// Implement this interface to make a custom implementation that will build a sitemap.
/// </summary>
public interface ISiteMapBuilder
{
ISiteMapNode BuildSiteMap(ISiteMap siteMap, ISiteMapNode rootNode);
}
}
ISiteMapBuilder can be configured in a sequence so that more than one implementation can be used to construct a single tree. The root node must be returned from every BuildSiteMap() call, but any one of the implementations can decide which node the root node will be.
Using ISiteMapBuilder typically requires the use of several related classes in concert. Here is a listing of some of the classes that are useful to use with ISiteMapBuilder:
Class | Useful Members | Purpose |
---|---|---|
SiteMapNodeKeyGenerator | GenerateKey() | Used to build a unique key based on several values of the node. |
SiteMapNodeFactory | Create() | Used to create instances of sitemap nodes. |
ISiteMap | AddNode() RemoveNode() |
Used to arrange instances of sitemap nodes into a tree. A class implementing this interface is automatically passed to the ISiteMapBuilder.BuildSiteMap() method. |
DynamicNodeBuilder | BuildDynamicNodesFor() | Can be used to allow your ISiteMapBuilder implementation to use existing IDynamicNodeProvider implementations to extend its functionality. |
To see an example of ISiteMapBuilder, have a look at the [XmlSiteMapBuilder] (https://github.com/maartenba/MvcSiteMapProvider/blob/v4/src/MvcSiteMapProvider/MvcSiteMapProvider/Builder/XmlSiteMapBuilder.cs) that builds the tree based on an XML structure, or the other implementations that ship with MvcSiteMapProvider: [ReflectionSiteMapBuilder] (https://github.com/maartenba/MvcSiteMapProvider/blob/v4/src/MvcSiteMapProvider/MvcSiteMapProvider/Builder/ReflectionSiteMapBuilder.cs), [VisitingSiteMapBuilder] (https://github.com/maartenba/MvcSiteMapProvider/blob/v4/src/MvcSiteMapProvider/MvcSiteMapProvider/Builder/VisitingSiteMapBuilder.cs), and [AspNetSiteMapBuilder] (https://github.com/maartenba/MvcSiteMapProvider/blob/v4/src/MvcSiteMapProvider/MvcSiteMapProvider/Builder/AspNetSiteMapBuilder.cs).
To register your ISiteMapBuilder implementation, you must use an external DI container. Here is a sample of how to register a single and multiple instances of ISiteMapBuilder using StructureMap:
// Single builder registration
var builder = this.For<ISiteMapBuilder>().Use<CustomSiteMapBuilder>();
// Multiple builder registration
var builder = this.For<ISiteMapBuilder>().Use<CompositeSiteMapBuilder>()
.EnumerableOf<ISiteMapBuilder>().Contains(y =>
{
y.Type<CustomSiteMapBuilder>();
y.Type<AnotherCustomSiteMapBuilder>();
y.Type<VisitingSiteMapBuilder>();
});
// Configure the builder set, using either one of the above builders
this.For<ISiteMapBuilderSetStrategy>().Use<SiteMapBuilderSetStrategy>()
.EnumerableOf<ISiteMapBuilderSet>().Contains(x =>
{
x.Type<SiteMapBuilderSet>()
.Ctor<string>("instanceName").Is("default")
.Ctor<bool>("securityTrimmingEnabled").Is(true)
.Ctor<bool>("enableLocalization").Is(true)
.Ctor<ISiteMapBuilder>().Is(builder)
.Ctor<ICacheDetails>().Is(cacheDetails);
});
See [Configuring MvcSiteMapProvider] (https://github.com/maartenba/MvcSiteMapProvider/wiki/Configuring-MvcSiteMapProvider#using-an-external-di-container) for more details on how to configure MvcSiteMapProvider using an external DI container.
Want to contribute? See our Contributing to MvcSiteMapProvider guide.
- Upgrading from v3 to v4
- Routing Basics
- Configuring MvcSiteMapProvider
- Defining Sitemap Nodes in XML
- Defining Sitemap Nodes using .NET Attributes
- Defining Sitemap Nodes using IDynamicNodeProvider
- HtmlHelper Extensions
- Controlling URL Behavior
- Using Action Filter Attributes
- Sitemaps XML Protocol Endpoint for Search Engines
- Using Custom Attributes on a Node
- Specifying Node Order
- Advanced Node Visibility
- Multiple Navigation Paths to a Single Page
- Multiple Sitemaps in One Application
- Security Trimming
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