MVC Sitemap Provider tutorial and examples

The ASP.net MVC sitemap provider is a solution aiming to provide your website with a fully functioning set of sitemap tools, such as breadcrumbs and node navigation.

When delving into using this solution, I made the huge mistake of first not reading up about ASP.net sitemaps in general. If you do not have any previous experience in using sitemaps on an ASP.net website, I would strongly recommend reading this MSDN article on ASP.net sitemaps. Many of the principles used in the ASP.net MVC framework are the same and reading this article will give you some fundamental basic principles.

Once you get the ASP.net MVC sitemap provider downloaded and registered, you should see that a sitemap file (Mvc.sitemap) has been created in your MVC solution’s root:

Site Map In Solution

Next, you need to go about editing your MVC.sitemap file so that it actually reflects the pages on your web site that are available to the user. This is the important bit – if you don’t get this right, the sitemap provider will not work as expected. All nodes in your file must:

  1. Be wrapped in an overall parent node representing the home page of your website.
    After all, the homepage is always the first point of call into any web application
  2. Point to REAL controllers and REAL actions.
    If you fill up your MVC.sitemap file with dummy nodes pointing to non existent controllers and actions, asp.net MVC site provider will not return these nodes in any rendering of the sitemap. You will not get an error message, but will just not see your nodes.
  3. Not point to a controller and an action that is already pointed to by a previous node.
    Again, the asp.net MVC sitemap provider will just remove any duplicate instances at runtime. You will not get an error message, but will just not see your nodes.

Here is an example of an MVC.sitemap file that is in a good format and is readable to the ASP.net MVC sitemap provider (apologies if the spacing has not worked correctly):

<?xml version="1.0" encoding="utf-8" ?>
<mvcSiteMap xmlns="http://mvcsitemap.codeplex.com/schemas/MvcSiteMap-File-3.0" enableLocalization="false">
  <mvcSiteMapNode title="Home" controller="Home" action="Index" changeFrequency="Always" updatePriority="Normal">
  <mvcSiteMapNode title="Home" controller="Home" action="Test" description="Home">
  <mvcSiteMapNode title="Dashboard" controller="Home" action="Dashboard"/>
  <mvcSiteMapNode title="My Profile" controller="Profile" action="MyProfile"/>
  <mvcSiteMapNode title="My Jobs" controller="Profile" action="MyJobs"/>
 </mvcSiteMapNode>
 <mvcSiteMapNode title="Workplace" controller="Workplace" action="Index" description="users">
  <mvcSiteMapNode title="Calendar" controller="Workplace" action="Calendar"/>
  <mvcSiteMapNode title="Customers" controller="Workplace" action="Customers"/>
  <mvcSiteMapNode title="Equipment" controller="Workplace" action="Equipment"/>
 </mvcSiteMapNode>
</mvcSiteMap>

Now, all you need to do is get the call to display your menu correct. If you get this wrong, you will end up displaying nodes that you don’t want to display. Here’s the call that worked for me on a file in a Mvc.Sitemap file that is essentially the same as the code listing above. This is in Razor syntax:

@Html.MvcSiteMap().Menu(false, true, false)

This call is telling the MvcSiteMap to display a menu not starting from the current node (the page the client is currently viewing), starting from the first child node, and hiding the overall starting node. This call has worked for me, but you may wish to tweak it depending on what exactly you want.

I will shortly put up a post about customising the way that the ASP.net MVC Sitemap provider displays a menu or breadcrumb trail.

Update

A more in-depth tutorial is now available from here. It covers starting from scratch, displaying navigation, breadcrumbs, and customising their appearance.

Links:

http://mvcsitemap.codeplex.com/

http://msdn.microsoft.com/en-us/library/yy2ykkab.aspx

 

Recommended Reading:

Pro ASP.NET MVC 3 Framework

 

 

 

 

About these ads

21 comments

  1. I’m waiting for your next tutorials.
    how to use menu() mathod?
    how tio use displayTemplates?
    and others

    thank you!

  2. where to locate the “displayTemplates” files and how to use ?

    in /view/shared/displayTemplates
    or in /view/displayTemplates

    • Hi,

      I had some trouble with this too. Apparently there was an issue with the NuGet package, which meant the displayTemplates were being put into the wrong location.

      It should be Views/Shared/DisplayTemplates

      I hope this helps!

  3. Just wondering-
    Say I have a “search” controller, with an action of “results”
    This takes a string q (which is passed in from QueryString – my url looks like
    /search?q=something)

    I’m trying to get my breadcrumb to show something like:

    Home > Search > Results for ‘something’

    Is that possible

      • Good morning, I want change separator character of the Breadcrumb from
        Home > Tools > ToolDetail1 to Home – Tools – ToolDetail1, but any changes in SiteMapPathHelperModel.cshtml file dont have any effect to my breadcrumb. Can you help me?

        thanks in advance and sorry for my english

  4. This sitemap provider is absolutely terrible. Bloated, unintiutive, does not seem to want to work for the simplest of tasks. We ended up writing our own, avoid this like the plague, unless you want to waste half a day wrestling with it.

  5. If you need to display a sitemap starting from a specific node. For example “My Profile” and all nodes underneath that. How do you specify you wish to start from that node only?

  6. Pingback: MVC SQL Sitemap Provider

  7. Pingback: Getting started with MvcSiteMapProvider with ASP.NET MVC | a developer's notes - a semi-technical web development BLOG

  8. this part is not closed
    mvcSiteMapNode title=”Home” controller=”Home” action=”Index” changeFrequency=”Always…..


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