asp.net-mvcsitemapmvcsitemapproviderasp.net-mvc-sitemap

Check if .NET MVC Sitemap exists


I am using

 <div class="row">
    <div class="col-lg-12 breadcrumb" >
        @Html.MvcSiteMap().SiteMapPath("")
     </div>
 </div>

to display the sitemap of current page. How do i check if the Sitemap exists for the Current page and then only display it?

like:

@if([Check Exists]){
 <div class="row">
    <div class="col-lg-12 breadcrumb" >
        @Html.MvcSiteMap().SiteMapPath("")
     </div>
 </div>
}

Solution

  • By default, the SiteMapPath HTML helper already does this check. If there is no node that corresponds to the current request the SiteMap.CurrentNode property will be null. When that happens, no HTML will be output from the SiteMapPath HTML helper.

    If that isn't good enough to cover your particular use case, you can use the built-in FilteredSiteMapNodeVisibilityProvider, a custom visibility provider, or security trimming to hide the nodes you don't want visible.

    Failing that, you could create a custom partial view template for the SiteMapPath or create a custom HTML helper based on the SiteMapPath if nothing else meets your needs.