Skip to content

Defining Sitemap Nodes using .NET Attributes

Shad Storhaug edited this page Feb 10, 2014 · 3 revisions

In the spirit of MVC, we have created a way to build convention-based nodes by specifying them as method attributes. This makes it convenient to keep the node information in the same place as your controller action. It can be accomplished by decorating your controller action methods with MvcSiteMapNodeAttribute and specifying the necessary properties. Note that every property that is available in XML is also available on MvcSiteMapNodeAttribute and each has the same purpose as if you were defining it in XML.

Controller and Action properties are missing, as those are defined implicitly based on where you place your MvcSiteMapNodeAttribute.

// GET: /Checkout/Index 
[MvcSiteMapNode(Title = "Checkout", ParentKey = "Store", Key = "Checkout")] 
public ActionResult Index(int id) 
{ 
    // ... 
}

// GET: /Checkout/Complete 
[MvcSiteMapNode(Title = "Checkout complete", ParentKey = "Checkout", Key = "Checkout_Complete")] 
public ActionResult Complete(int id) 
{ 
    // ... 
}

To use this feature, you need to ensure it is both enabled and the assembly where your nodes are located is specified in the IncludeAssembliesForScan in the configuration. If all of your controllers are in your MVC assembly, everything is done for you when you install the NuGet package. However, if your controllers are in a different assembly, you need to add (or change) the assembly name in the configuration.

If using internal DI, these settings are found in the web.config file (or you may add them if they are missing). The MvcSiteMapProvider_IncludeAssembliesForScan value must be a comma delimited list of .NET assembly names. Spaces are not allowed after the comma.

<appSettings> 
    <add key="MvcSiteMapProvider_IncludeAssembliesForScan" value="MyAssemblyName,MyOtherAssemblyName" />
    <add key="MvcSiteMapProvider_ScanAssembliesForSiteMapNodes" value="true" />
</appSettings>

Using external DI, you need to add the assembly to the includeAssembliesForScan variable near the top of the file. By default, it is enabled by registering the ReflectionSiteMapBuilder in the configuration.

string[] includeAssembliesForScan = new string[] { "MyAssemblyName", "MyOtherAssemblyName" };

StructureMap example shown:

var builder = this.For<ISiteMapBuilder>().Use<CompositeSiteMapBuilder>()
    .EnumerableOf<ISiteMapBuilder>().Contains(y =>
    {
        y.Type<XmlSiteMapBuilder>()
            .Ctor<ISiteMapXmlReservedAttributeNameProvider>().Is(reservedAttributeNameProvider)
            .Ctor<IXmlSource>().Is(xmlSource);
			
        // ReflectionSiteMapBuilder is what scans your assembly for MvcSiteMapNodeAttribute.
        // Removing it from your DI configuration will disable it.
        y.Type<ReflectionSiteMapBuilder>()
            .Ctor<IEnumerable<string>>("includeAssemblies").Is(includeAssembliesForScan)
            .Ctor<IEnumerable<string>>("excludeAssemblies").Is(new string[0]);
        y.Type<VisitingSiteMapBuilder>();
    });

Key and ParentKey

Except for the node that represents the root node, you must include the ParentKey for every node. This ParentKey must match the Key property of the node you want to use as a parent node. In most cases, you should specify Key as well as ParentKey explicitly on each MvcSiteMapNodeAttribute.

Only 1 node can be defined without a ParentKey in your configuration and that node automatically is considered to be the root node. Most of that time the root node will need to be configured in your Mvc.sitemap file.

However, if you want to eliminate the Mvc.sitemap XML configuration entirely, it is possible to remove the XmlSiteMapBuilder from your configuration (external DI only). See #197 for details and a working demo.


Want to contribute? See our [Contributing to MvcSiteMapProvider] (https://github.com/maartenba/MvcSiteMapProvider/blob/master/CONTRIBUTING.md) guide.



[Version 3.x Documentation] (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