In this recipe, we are going to take a look at how to implement a breadcrumb trail using the open source project MvcSiteMap
.
The key to making this recipe work is getting the latest from the MvcSiteMap
project here: http://mvcsitemap.codeplex.com/.
MvcSiteMap
. web.config
file and add a new provider for the SiteMap
. You can add this directly after the <system.web>
block.Web.config:
<siteMap defaultProvider="MvcSiteMapProvider" enabled="true"> <providers> <clear /> <add name="MvcSiteMapProvider" type="MvcSiteMapProvider.DefaultSiteMapProvider, MvcSiteMapProvider" siteMapFile="~/Mvc.Sitemap" securityTrimmingEnabled="true" cacheDuration="5" enableLocalization="true" scanAssembliesForSiteMapNodes="true" skipAssemblyScanOn="" attributesToIgnore="bling" nodeKeyGenerator=" MvcSiteMapProvider.DefaultNodeKeyGenerator, MvcSiteMapProvider" controllerTypeResolver=" MvcSiteMapProvider.DefaultControllerTypeResolver, MvcSiteMapProvider" actionMethodParameterResolver=" MvcSiteMapProvider.DefaultActionMethodParameterResolver, MvcSiteMapProvider" aclModule="MvcSiteMapProvider.DefaultAclModule, MvcSiteMapProvider" siteMapNodeUrlResolver=" MvcSiteMapProvider.DefaultSiteMapNodeUrlResolver, MvcSiteMapProvider" siteMapNodeVisibilityProvider=" MvcSiteMapProvider.DefaultSiteMapNodeVisibilityProvider, MvcSiteMapProvider" /> </providers> </siteMap>
SiteMap
file to your application in the root called Mvc.sitemap
.Web.sitemap:
<?xml version="1.0" encoding="utf-8" ?> <mvcSiteMap xmlns="http://mvcsitemap.codeplex.com/schemas/ MvcSiteMap-File-2.0" enableLocalization="true"> <mvcSiteMapNode title="Home" controller="Home" action="Index" changeFrequency="Always" updatePriority="Normal"> <mvcSiteMapNode title="Edit Person" controller="Home" action="Edit" /> </mvcSiteMapNode> </mvcSiteMap>
MvcSiteMap
. We will add a breadcrumb trail by adding a call to MvcSiteMap().SiteMapPath()
in the Site.Master
.Views/Shared/Site.Master: <div id="main"> <%= Html.MvcSiteMap().SiteMapPath(" > ") %> <asp:ContentPlaceHolder ID="MainContent" runat="server" />
Web.config
.<add namespace="MvcSiteMapProvider.Web.Html" />
<add namespace="MvcSiteMapProvider.Web.Html.Models" />
</namespaces>
</pages>
Maarten Balliauw (http://blog.maartenballiauw.be/) had one of the first custom providers to take care of the lack of good support for the concept of the ASP.NET SiteMap back in 2008 (http://blog.maartenballiauw.be/post/2008/08/29/Building-an-ASPNET-MVC-sitemap-provider-with-security-trimming.aspx). He has since expanded on that little blog post to create a full blown open source project with loads of additional functionality. This code is available at http://mvcsitemap.codeplex.com/, if you are curious about the internals of it.
For an idea of all the nodes that you have access to with this SiteMap provider, take a look here: http://mvcsitemap.codeplex.com/wikipage?title=Creating%20a%20first%20sitemap&referringTitle=Home.