-
Notifications
You must be signed in to change notification settings - Fork 218
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.
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>
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.
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 |
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 %>
<% } %>
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.
- Registering the provider
- Creating a first sitemap
- Defining sitemap nodes in code
- Dynamic sitemaps
- HtmlHelper functions
- Action Filter Attributes
- Exporting the sitemap for search engine indexing
- Advanced node visibility
- Extension points
Other places around the web have some documentation that is helpful for getting started and finding answers that are not found here.
- MvcSiteMapProvider 4.0 - A Test Drive
- MvcSiteMapProvider 4.0 - SEO Features Tutorial
- How to Make MvcSiteMapProvider Remember a User’s Position
- MvcSiteMapProvider 4.0 - Cache Configuration
- MvcSiteMapProvider 4.0 - Extending the Cache
- MvcSiteMapProvider 4.0 - Unit Testing with the SiteMaps Static Methods
- Debugging an MvcSiteMapProvider Configuration
- Converting from C# to Vb MvcSiteMapProvider
- ASP.NET MVC Menu using Site Map Provider & Bootstrap 3 Navbar
- ASP.NET MVC SiteMapPath using Site Map Provider & Bootstrap Breadcrumbs
- NightOwl888's MvcSiteMapProvider Demos - Filter for "MvcSiteMapProvider" to see the most relevant.
- MvcSiteMapProvider Tutorial and Examples
- MvcSiteMapProvider Tutorial 2 - Breadcrumbs
- Getting Started with MvcSiteMapProvider
- Inside the MvcSiteMapProvider - Part 1
- Inside the MvcSiteMapProvider - Part 2: Dynamic node providers
- Inside the MvcSiteMapProvider - Part 3: The ISiteMapVisibilityProvider
- Inside the MvcSiteMapProvider - Part 4: The IAclModule
- Inside the MvcSiteMapProvider - Part 5: The ISiteMapNodeUrlResolver
- Styling MvcSiteMapProvider with CSS
- Using MvcSiteMapProvider with Twitter Bootstrap
- ASP.NET MVC Menu using Site Map Provider & Bootstrap Navbar