-
Notifications
You must be signed in to change notification settings - Fork 1
Defining Sitemap Nodes using .NET Attributes
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>();
});
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.
- [Wiki Home] (.)
- [Release Notes] (https://github.com/maartenba/MvcSiteMapProvider/releases)
Want to contribute? See our [Contributing to MvcSiteMapProvider] (https://github.com/maartenba/MvcSiteMapProvider/blob/master/CONTRIBUTING.md) guide.
- [Upgrading from v3 to v4] (Upgrading-from-v3-to-v4)
- [Routing Basics] (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] (Multiple-Sitemaps-in-One-Application)
- [Security Trimming] (Security-Trimming)
[Version 3.x Documentation] (Version-3.x-Documentation)
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] (http://www.shiningtreasures.com/post/2013/08/07/MvcSiteMapProvider-40-a-test-drive)
- [MvcSiteMapProvider 4.0 - SEO Features Tutorial] (http://www.shiningtreasures.com/post/2013/08/10/mvcsitemapprovider-4-seo-features)
- [How to Make MvcSiteMapProvider Remember a User’s Position] (http://www.shiningtreasures.com/post/2013/09/02/how-to-make-mvcsitemapprovider-remember-a-user-position)
- [MvcSiteMapProvider 4.0 - Cache Configuration] (http://www.shiningtreasures.com/post/2013/08/11/mvcsitemapprovider-4-cache-configuration)
- [MvcSiteMapProvider 4.0 - Extending the Cache] (http://www.shiningtreasures.com/post/2013/08/13/mvcsitemapprovider-4-extending-the-cache)
- [MvcSiteMapProvider 4.0 - Unit Testing with the SiteMaps Static Methods] (http://www.shiningtreasures.com/post/2013/08/14/mvcsitemapprovider-4-unit-testing-with-the-sitemaps-static-methods)
- [Debugging an MvcSiteMapProvider Configuration] (http://www.shiningtreasures.com/post/2013/08/21/debugging-an-mvcsitemapprovider-configuration)
- [Converting from C# to Vb MvcSiteMapProvider] (http://www.developerfusion.com/thread/112710/converting-from-c-to-vb-mvcsitemapprovider/)
- [NightOwl888's MvcSiteMapProvider Demos] (https://github.com/NightOwl888?tab=repositories) - Filter for "MvcSiteMapProvider" to see the most relevant.
- [MvcSiteMapProvider Tutorial and Examples] (http://edspencer.me.uk/2011/02/10/mvc-sitemap-provider-tutorial/)
- [MvcSiteMapProvider Tutorial 2 - Breadcrumbs] (http://edspencer.me.uk/2011/09/20/mvc-sitemap-provider-tutorial-2-breadcrumbs/)
- [Getting Started with MvcSiteMapProvider] (http://blog.danstuken.com/2011/04/29/getting-started-with-mvcsitemapprovider/)
- [Inside the MvcSiteMapProvider - Part 1] (http://xharze.blogspot.com/2012/04/inside-mvcsitemapprovider-part-1.html)
- [Inside the MvcSiteMapProvider - Part 2: Dynamic node providers] (http://xharze.blogspot.com/2012/04/inside-mvcsitemapprovider-part-2.html)
- [Inside the MvcSiteMapProvider - Part 3: The ISiteMapVisibilityProvider] (http://xharze.blogspot.com/2012/04/inside-mvcsitemapprovider-part-3.html)
- [Inside the MvcSiteMapProvider - Part 4: The IAclModule] (http://xharze.blogspot.com/2012/04/inside-mvcsitemapprovider-part-4.html)
- [Inside the MvcSiteMapProvider - Part 5: The ISiteMapNodeUrlResolver] (http://xharze.blogspot.com/2012/04/inside-mvcsitemapprovider-part-5.html)
- [Styling MvcSiteMapProvider with CSS] (http://tutsblog.net/styling-mvc-sitemap-provider-with-css/)
- [Using MvcSiteMapProvider with Twitter Bootstrap] (http://codingit.wordpress.com/2013/05/03/using-mvcsitemapprovider-with-twitter-bootstrap/)
- [ASP.NET MVC Menu using Site Map Provider & Bootstrap Navbar] (http://joeylicc.wordpress.com/2013/06/04/asp-net-mvc-menu-using-site-map-provider-bootstrap-navbar/)
- [StackOverflow MvcSiteMapProvider] (http://stackoverflow.com/questions/tagged/mvcsitemapprovider)
- [StackOverflow MvcSiteMap] (http://stackoverflow.com/questions/tagged/mvcsitemap)
- [StackOverflow ASP.NET MVC SiteMap] (http://stackoverflow.com/questions/tagged/asp.net-mvc-sitemap)
- [CodePlex Discussion Forum (no longer maintained)] (http://mvcsitemap.codeplex.com/discussions/topics/general?size=2147483647)
- [Telerik Forum] (http://www.telerik.com/search.aspx?insection=False&start=0&client=telerik_developer_tools&q=MvcSiteMapProvider&sid=1)
- [maartenba's blog] (http://blog.maartenballiauw.be/search.aspx?q=mvcsitemapprovider)
- [NightOwl888's blog] (http://www.shiningtreasures.com/category/MvcSiteMapProvider)