Skip to content

Defining Sitemaps using ISiteMapBuilder

Shad Storhaug edited this page Feb 10, 2014 · 1 revision

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).

Registering ISiteMapBuilder

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.



Version 3.x Documentation


Unofficial Documentation and Resources

Other places around the web have some documentation that is helpful for getting started and finding answers that are not found here.

Tutorials and Demos

Version 4.x
Version 3.x

Forums and Q & A Sites

Other Blog Posts

Clone this wiki locally