-
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.
Note: "visibility" is a custom attribute, and must be added to the
Attributes
property when usingMvcSiteMapNodeAttribute
. For example:[MvcSiteMapNode(Title = "Some Page", Attributes = @"{ ""visibility"": ""MenuHelper,!*"" }")]
. Key names are case sensitive.
Controller
and Action
properties are missing, as those are defined implicitly based on where you place your MvcSiteMapNodeAttribute
(similar to when you use Attribute Routing in MVC).
// 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, ensure it is both enabled and the assembly where your nodes are located is specified in the IncludeAssembliesForScan
setting 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.
The 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>
Note: With the default configuration, you must put the home page node in the
Mvc.sitemap
file. You can disable XML node configuration by setting theMvcSiteMapProvider_EnableSiteMapFile
web.config setting tofalse
. Once this is done, you can safely eliminate theMvc.sitemap
andMvcSiteMapSchema.xsd
files from your project.
This setting is a variable at the top of the MvcSiteMapProvider DI Module.
Note: The name of the module varies depending on the DI container and can be changed after installing the NuGet package, but each module is located under the
\DI\<containerName>\Modules\
folder in your project by default.
string[] includeAssembliesForScan = new string[] { "MyAssemblyName", "MyOtherAssemblyName" };
Later on in the file, the variable is passed into the constructor of the ReflectionSiteMapBuilder
class. This class must be registered for the MvcSiteMapNodeAttribute
declarations to function. Here is an example in StructureMap:
// Register the sitemap node providers
var siteMapNodeProvider = this.For<ISiteMapNodeProvider>().Use<CompositeSiteMapNodeProvider>()
.EnumerableOf<ISiteMapNodeProvider>().Contains(x =>
{
x.Type<XmlSiteMapNodeProvider>()
.Ctor<bool>("includeRootNode").Is(true)
.Ctor<bool>("useNestedDynamicNodeRecursion").Is(false)
.Ctor<IXmlSource>().Is(xmlSource);
x.Type<ReflectionSiteMapNodeProvider>()
.Ctor<IEnumerable<string>>("includeAssemblies").Is(includeAssembliesForScan) // <-- variable passed here
.Ctor<IEnumerable<string>>("excludeAssemblies").Is(new string[0]);
});
Similarly to internal DI, with external DI you can disable support for the Mvc.sitemap
file by eliminating it from your configuration. Once this is done, you can safely eliminate the Mvc.sitemap
and MvcSiteMapSchema.xsd
files from your project. All examples are shown for StructureMap. Other DI configurations are similar.
First, you need to remove the registration for RuntimeFileCacheDependency
and replace it with a NullCacheDependency
.
//var cacheDependency =
// this.For<ICacheDependency>().Use<RuntimeFileCacheDependency>()
// .Ctor<string>("fileName").Is(absoluteFileName);
var cacheDependency =
this.For<ICacheDependency>().Use<NullCacheDependency>();
Next, remove the XmlSource
registration.
// Prepare for our node providers
//var xmlSource = this.For<IXmlSource>().Use<FileXmlSource>()
// .Ctor<string>("fileName").Is(absoluteFileName);
Then, you need to remove the XmlSiteMapNodeProvider
from the configuration.
// Register the sitemap node providers
var siteMapNodeProvider = this.For<ISiteMapNodeProvider>().Use<CompositeSiteMapNodeProvider>()
.EnumerableOf<ISiteMapNodeProvider>().Contains(x =>
{
//x.Type<XmlSiteMapNodeProvider>()
// .Ctor<bool>("includeRootNode").Is(true)
// .Ctor<bool>("useNestedDynamicNodeRecursion").Is(false)
// .Ctor<IXmlSource>().Is(xmlSource);
x.Type<ReflectionSiteMapNodeProvider>()
.Ctor<IEnumerable<string>>("includeAssemblies").Is(includeAssembliesForScan)
.Ctor<IEnumerable<string>>("excludeAssemblies").Is(new string[0]);
});
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. The root node will always need to be configured in your Mvc.sitemap
file unless you disable XML support as shown above.
- [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)