This project is read-only.

HtmlHelper functions


The MvcSiteMapProvider provides different HtmlHelper extension methods which you can use to generate SiteMap-specific HTML code on your ASP.NET MVC views. Here's a list of available HtmlHelper extension methods.
  • Html.MvcSiteMap().Menu() - Can be used to generate a menu
  • Html.MvcSiteMap().SiteMap() - Can be used to generate a list of all pages in your sitemap
  • Html.MvcSiteMap().SiteMapPath() - Can be used to generate a so-called "breadcrumb trail"
  • Html.MvcSiteMap().SiteMapTitle() - Can be used to render the current SiteMap node's title
Note that these should be registered in the appropriate Web.config for your view engine.

Registering MvcSiteMapProvider HtmlHelper functions with Webforms ViewEngine

In the root Web.config file, add the following under the <pages> element:

        <! -- ... --> 
        <! -- ... --> 
        <add namespace="MvcSiteMapProvider.Web.Html" /> 
        <add namespace="MvcSiteMapProvider.Web.Html.Models" /> 

Registering MvcSiteMapProvider HtmlHelper functions with Razor ViewEngine

In the Web.config file under the Views folder, add the following under the <system.web.webPages.razor> element:

  <! -- ... -->
  <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <! -- ... --> 
      <add namespace="MvcSiteMapProvider.Web.Html" /> 
      <add namespace="MvcSiteMapProvider.Web.Html.Models" /> 

Customizing rendered output

All helpers in MvcSiteMapProvider are make use of templates: whenever a node in a menu is to be rendered, a specific partial view is used to render this node. This is based on the idea of templated helpers.
The default templates that are used can be found on the downloads page. Locate them under the Views/DisplayTemplates folder of your project to be able to customize them.

Available models

When creating your own templates for MvcSiteMapProvider's helpers, the following model objects are used and can be templated:

HtmlHelper Models used
Html.MvcSiteMap().Menu() MvcSiteMapProvider.Web.Html.Models.MenuHelperModel and MvcSiteMapProvider.Web.Html.Models.SiteMapNodeModel
Html.MvcSiteMap().SiteMap() MvcSiteMapProvider.Web.Html.Models.SiteMapHelperModel and MvcSiteMapProvider.Web.Html.Models.SiteMapNodeModel
Html.MvcSiteMap().SiteMapPath() MvcSiteMapProvider.Web.Html.Models.SiteMapPathHelperModel and MvcSiteMapProvider.Web.Html.Models.SiteMapNodeModel
Html.MvcSiteMap().SiteMapTitle() MvcSiteMapProvider.Web.Html.Models.SiteMapTitleHelperModel

Example template

The following template is an example for rendering a sitemap node represented by the MvcSiteMapProvider.Web.Html.Models.SiteMapNodeModel model.

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MvcSiteMapProvider.Web.Html.Models.SiteMapNodeModel>" %>
<%@ Import Namespace="System.Web.Mvc.Html" %>

<% if (Model.IsCurrentNode && Model.SourceMetadata["HtmlHelper"].ToString() != "MvcSiteMapProvider.Web.Html.MenuHelper")  { %>
    <%=Model.Title %>
<% } else if (Model.IsClickable) { %>
    <a href="<%=Model.Url %>"><%=Model.Title %></a>
<% } else { %>
    <%=Model.Title %>
<% } %>

Known performance issues and the solution

A performance degradation may be noticed working with HtmlHelper functions from Visual Studio. This is because during debugging, no caching occurs internally in ASP.NET MVC regarding view rendering. The solution to this is running the application in release mode or changing Web.config to run under release mode:

<compilation debug="false">

See Simone Chiaretta's blog for a detailed explanation on this.

Last edited Jan 6, 2011 at 7:58 AM by maartenba, version 8


Islam_Ibrahim Oct 29, 2011 at 9:25 AM 
Is there such a way to create a Menu hierarchy of a specific area, like Html.MvcSiteMap().Menu(area: "Admin")?

jao28 May 2, 2011 at 12:39 AM 
For those of you who might be having trouble in modifying the templates as defined in the "Customizing rendered output" section above and getting them to work, I have found that you should actually locate the templates under Views/Shared/DisplayTemplates instead of Views/DisplayTemplates as is mentioned above. Hope this helps someone.