ASP.NET MVC SiteMapPath using Site Map Provider & Bootstrap Breadcrumbs

1. Install Bootstrap for MVC 4 NuGet package. This package will install Twitter Bootstrap to the project and add bundling and minification to application start.

2. Install MvcSiteMapProvider NuGet package. This package is a SiteMapProvider implementation for the ASP.NET MVC framework.

3. In web.config, downsize the MvcSiteMapProvider configuration to the minimal. Keep it simple, stupid. (KISS)

<system.web>
  <siteMap defaultProvider="MvcSiteMapProvider">
    <providers>
      <clear />
      <add name="MvcSiteMapProvider" type="MvcSiteMapProvider.DefaultSiteMapProvider, MvcSiteMapProvider" siteMapFile="~/Mvc.Sitemap" />
    </providers>
  </siteMap>
</system.web>

4. Modify the ~\Mvc.sitemap file as the following.

<?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-3.0"
            xsi:schemaLocation="http://mvcsitemap.codeplex.com/schemas/MvcSiteMap-File-3.0 MvcSiteMapSchema.xsd"
            enableLocalization="true">
  <mvcSiteMapNode title="Home" controller="Home" action="Index">
    <mvcSiteMapNode title="About" controller="Home" action="About" />
    <mvcSiteMapNode title="Contact" controller="Home" action="Contact" />
    <mvcSiteMapNode title="Administration" clickable="false">
      <mvcSiteMapNode title="User Mgmt" controller="Home" action="UserMgmt" />
      <mvcSiteMapNode title="Role Mgmt" controller="Home" action="RoleMgmt" />
    </mvcSiteMapNode>
    <mvcSiteMapNode title="Profile" clickable="false">
      <mvcSiteMapNode title="Change Password" controller="Home" action="ChangePassword" />
      <mvcSiteMapNode title="Separator" clickable="false" />
      <mvcSiteMapNode title="Sign Off" controller="Home" action="SignOff" />
    </mvcSiteMapNode>
  </mvcSiteMapNode>
</mvcSiteMap>

5. The Twitter Bootstrap package installed in step 1 wouldn’t add the style and script rendering to the layout file. You need to do it manually by modifying the _Layout.cshtml file as following.

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width" />
  <title>@ViewBag.Title</title>
  @Styles.Render("~/Content/css")
  @Styles.Render("~/Content/bootstrap")
  @Scripts.Render("~/bundles/modernizr")
</head>
<body>
  @RenderBody()

  @Scripts.Render("~/bundles/jquery")
  @Scripts.Render("~/bundles/bootstrap")
  @RenderSection("scripts", required: false)
</body>
</html>

6. Add a partial view file BootstrapSiteMapPathHelperModel.cshtml under ~\Views\Shared\DisplayTemplates to render the Site Map Path which fits Bootstrap’s Breadcrumbs specifications.

@model MvcSiteMapProvider.Web.Html.Models.SiteMapPathHelperModel
@using System.Web.Mvc.Html
@using System.Linq
@using MvcSiteMapProvider.Web.Html.Models

@if (Model.Nodes.Count != 1)
{
    @:
<ul class="breadcrumb">
foreach (var node in Model.Nodes)
{
    if (node == Model.Nodes.First())
    {
        continue;
    }

    if (node != Model.Nodes.Last())
    {
        string url = node.IsClickable ? node.Url : "#";

            @:
	<li><a href="@url">@node.Title</a><span class="divider">></span></li>
}
    else
    {
            @:
	<li class="active">@node.Title</li>
}
}

    @:</ul>
}

7. Add HomeController, and then add Index, About, Contact, UserMgmt, RoleMgmt & ChangePassword Views.

8. Edit the _Layout.cshtml file again to render the MvcSiteMap’s site map path using the Bootstrap site map path partial view.

......
<body>
	<div class="container">
		<div class="row">
			<div class="span12">
				<nav>
					@Html.MvcSiteMap().Menu("BootstrapMenuHelperModel")
				</nav>
			</div>
		</div>
		<!-- //row -->
		<div class="row">
			<div class="span12">
				@Html.MvcSiteMap().SiteMapPath("BootstrapSiteMapPathHelperModel")
			</div>
		</div>
		<!-- //row -->
		<div class="row">
			<div class="span12">
				@RenderBody()
			</div>
		</div>
		<!-- //row -->
	</div>
	......
</body>
......

9. Completed!!!

About Site Map Path
About Site Map Path

Administration > User Mgmt Site Map Path
Administration > User Mgmt Site Map Path

Profile > Change Password Site Map Path
Profile > Change Password Site Map Path

You can download the code sample here.

References

Advertisements

7 thoughts on “ASP.NET MVC SiteMapPath using Site Map Provider & Bootstrap Breadcrumbs

  1. One question: What if I want to add a Home link (e.g. Home > About) to go back to my Home page? It seems like the first level of my sitemap xml document is ignored.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s