Leaving breadcrumbs for your users with MvcSiteMap

In this recipe, we are going to take a look at how to implement a breadcrumb trail using the open source project MvcSiteMap.

Getting ready

The key to making this recipe work is getting the latest from the MvcSiteMap project here: http://mvcsitemap.codeplex.com/.

How to do it...

  1. Kick off from a copy of the last recipe, or a new project.
  2. Add a reference to the MvcSiteMap.
  3. Then open up your 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>
    
  4. Now you can add a SiteMap file to your application in the root called Mvc.sitemap.

    Note

    The name is important, because that is what is configured in the site map provider configuration!

  5. In the new site map file, you can select all the existing code and delete it. Then enter the following site map, which corresponds to the default application's controllers and views (Home, Account, LogOn, Register, and so on).

    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>
    
  6. Now all you need to do is to start using the functionality that is offered with the 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(" &gt; ") %>
    <asp:ContentPlaceHolder ID="MainContent" runat="server" />
    
  7. Add the following line to your Web.config.
    <add namespace="MvcSiteMapProvider.Web.Html" />
    <add namespace="MvcSiteMapProvider.Web.Html.Models" />
    </namespaces>
    </pages>
    
  8. Now you can build the application and click around. You should see the breadcrumb trail build itself as you move from one view to the next.
    How to do it...

How it works...

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.

There's more...

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset