-
Notifications
You must be signed in to change notification settings - Fork 1
Getting Started
The NuGet package has added the default configuration to your application. Unless you are using .NET 3.5 or have installed a DI modules-only package, this configuration will start up automatically.
However, you still need to add the HTML helpers to your views to see it work. Let's get started by adding the following 2 HTML helpers to your /Views/Shared/_Layout.cshtml or /Views/Shared/_Layout.aspx page.
@Html.MvcSiteMap().Menu()
@Html.MvcSiteMap().SiteMapPath()
If you run your project, you will now see a menu that looks something like this:
Home About
And a breadcrumb trail that just shows:
Home
If you click the About link, you will see your breadcrumb trail change to the following (we are assuming you have a controller action on the HomeController named About and a corresponding view named /Views/Home/About.cshtml).
Home > About
Next, let's add the Contact page to demonstrate how easy it is to add new pages to the configuration. Again, make sure your Home controller has a Contact action, like this:
public ActionResult Contact(int id)
{
ViewBag.Message = "Your contact page.";
return View();
}
And make sure you have added a view named /Views/Home/Contact.cshtml (or .aspx).
Open up the /Mvc.sitemap file and you will see that the default configuration looks like this.
<?xml version="1.0" encoding="utf-8" ?>
<mvcSiteMap xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://mvcsitemap.codeplex.com/schemas/MvcSiteMap-File-4.0"
xsi:schemaLocation="http://mvcsitemap.codeplex.com/schemas/MvcSiteMap-File-4.0 MvcSiteMapSchema.xsd">
<mvcSiteMapNode title="Home" controller="Home" action="Index">
<mvcSiteMapNode title="About" controller="Home" action="About"/>
</mvcSiteMapNode>
</mvcSiteMap>
Add another node just below about, setting the title to "Contact Us", the controller to "Home", and the action to "Contact" as shown below.
<?xml version="1.0" encoding="utf-8" ?>
<mvcSiteMap xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://mvcsitemap.codeplex.com/schemas/MvcSiteMap-File-4.0"
xsi:schemaLocation="http://mvcsitemap.codeplex.com/schemas/MvcSiteMap-File-4.0 MvcSiteMapSchema.xsd">
<mvcSiteMapNode title="Home" controller="Home" action="Index">
<mvcSiteMapNode title="About" controller="Home" action="About"/>
<mvcSiteMapNode title="Contact Us" controller="Home" action="Contact"/>
</mvcSiteMapNode>
</mvcSiteMap>
Run your project again, and you will see that your menu now contains a Contact Us link. Click it and you will see that the breadcrumb trail now shows the path below.
Home > Contact Us
Don't like XML configuration?
Don't like the HTML elements that are output from these HTML helpers? Change them!
Navigate to the /Views/Shared/DisplayTemplates/ folder and you will see a list of templates that are used to build the HTML, as follows.
- CanonicalHelperModel
- MenuHelperModel
- MetaRobotsHelperModel
- SiteMapHelperModel
- SiteMapNodeModel
- SiteMapNodeModelList
- SiteMapPathHelperModel
- SiteMapTitleHelperModel
To edit the menu, have a look at the MenuHelperModel.cshtml file. The default looks like this:
@model MvcSiteMapProvider.Web.Html.Models.MenuHelperModel
@using System.Web.Mvc.Html
@using MvcSiteMapProvider.Web.Html.Models
<ul id="menu">
@foreach (var node in Model.Nodes) {
<li>@Html.DisplayFor(m => node)
@if (node.Children.Any()) {
@Html.DisplayFor(m => node.Children)
}
</li>
}
</ul>
It calls the SiteMapNodeModel in a loop. But we can easily copy the contents of SiteMapNodeModel into MySiteMapNode and then edit the code. Then we can just call our new template by editing MenuHelperModel as follows.
@model MvcSiteMapProvider.Web.Html.Models.MenuHelperModel
@using System.Web.Mvc.Html
@using MvcSiteMapProvider.Web.Html.Models
<ul id="menu">
@foreach (var node in Model.Nodes) {
<li>@Html.DisplayFor(m => node)
@if (node.Children.Any()) {
@Html.DisplayFor(m => node.Children, "MySiteMapNode")
}
</li>
}
</ul>
In addition to giving you HTML helpers, MvcSiteMapProvider keeps track of each of your URLs so they can be submitted to search engines using the standardized Sitemaps XML format.
This feature is automatically enabled by default, just navigate to the path /sitemap.xml in your browser to see the output from your configured nodes.
MvcSiteMapProvider does a lot more than we can describe on one page, and it can also be extended in many ways - you can even replace much of the default behavior with your own code. Read the full documentation on our wiki, or for a tutorial visit MvcSiteMapProvider - A Test Drive.
For .NET 3.5, you need to manually add the code to launch MvcSiteMapProvider to your Application_Start() event of the /Global.asax file. There is only 1 line required.
MvcSiteMapProvider.DI.Composer.Compose();
After adding that line, MvcSiteMapProvider will launch when you run the application, as it would for .NET 4.0 or higher.
There is a readme file that opens up automatically when you install one of these packages with the code to add to your composition root. In case you need to reference it again, you can view the readme files online at the following locations.
- [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)