Skip to content

HtmlHelper Extensions

Shad Storhaug edited this page Apr 24, 2018 · 3 revisions

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
  • Html.MvcSiteMap().CanonicalTag() - Can be used to render the current SiteMap node's canonical tag for SEO purposes
  • Html.MvcSiteMap().MetaRobotsTag() - Can be used to render the current SiteMap node's meta robots tag for SEO purposes

Note that these should be registered in the appropriate Web.config for your view engine (if you install via Nuget, this will be done automatically).

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, MvcSiteMapProvider.Web.Html.Models.SiteMapNodeModelList and MvcSiteMapProvider.Web.Html.Models.SiteMapNodeModel
Html.MvcSiteMap().SiteMap() MvcSiteMapProvider.Web.Html.Models.SiteMapHelperModel, MvcSiteMapProvider.Web.Html.Models.SiteMapNodeModelList 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
Html.MvcSiteMap().CanonicalTag() MvcSiteMapProvider.Web.Html.Models.CanonicalHelperModel
Html.MvcSiteMap().MetaRobotsTag() MvcSiteMapProvider.Web.Html.Models.MetaRobotsHelperModel

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 3.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