Skip to content

HtmlHelper functions

Shad Storhaug edited this page Feb 9, 2014 · 1 revision

Overview

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:

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

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:

<system.web.webPages.razor>
  <! -- ... -->
  <pages pageBaseType="System.Web.Mvc.WebViewPage">
    <namespaces>
      <! -- ... --> 
      <add namespace="MvcSiteMapProvider.Web.Html" /> 
      <add namespace="MvcSiteMapProvider.Web.Html.Models" /> 
    </namespaces>
  </pages>
</system.web.webPages.razor>

## 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 templates are included when you install the package from NuGet. 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.


Want to contribute? See our Contributing to MvcSiteMapProvider guide.



Version 4.x Documentation


Unofficial Documentation and Resources

Other places around the web have some documentation that is helpful for getting started and finding answers that are not found here.

Tutorials and Demos

Version 4.x
Version 3.x

Forums and Q & A Sites

Other Blog Posts

Clone this wiki locally