Search code examples
asp.net-mvc-5drymvcsitemapprovider

How to provide default url for mvcSiteMapNodes


I have some nodes that are strictly navigation. So far I've handled this by using a "navigation node" route and explicitly setting the url (~/Home/*)on the nav only nodes. It works fine. This allows me to have a menu hierarchy without having to add dummy for each nav only menu item (which would be dumb) while allowing them to show up in the menu and breadcrumb hierarchy.

Mvc.sitemap

  <mvcSiteMapNode title="Home" controller="Home" action="Index" >
    <mvcSiteMapNode title="Products" url="~/Home/Products"   roles="*">
      <mvcSiteMapNode title="Shampoo MAX" url="~/Home/Products/ShampooMAX" >
        <mvcSiteMapNode title="Policies" controller="Object" action="List" type="Policy" >
        </mvcSiteMapNode>
        <mvcSiteMapNode title="Quotes"  controller="Object" action="List" >
        </mvcSiteMapNode>
        <mvcSiteMapNode title="Claims"  controller="Object" action="List">
        </mvcSiteMapNode>
      </mvcSiteMapNode>
    </mvcSiteMapNode>
    <mvcSiteMapNode title="Documents" controller="Document" action="Index" />
    <mvcSiteMapNode title="Forms" controller="Form" action="Index"/>
    <mvcSiteMapNode title="Links" url="~/Home/Links" roles="*">
      <mvcSiteMapNode url="https://interfarina.com/login#login" targetFrame="_blank"   title="Quoting" roles="*"/>
      <mvcSiteMapNode url="https://drive.google.com/file/d/asdfsdfsdfsdfs/view" targetFrame="_blank"   title="Downloadable App" roles="*"/>
      <mvcSiteMapNode url="https://gumbysafety.com/app/user/login" targetFrame="_blank"   title="Sales Tools" roles="*"/>
    </mvcSiteMapNode>
  </mvcSiteMapNode>

RouteConfig.cs

  routes.MapRoute(
      name: "Navigation-Only Node",
      url: "Home/{*url}",
      defaults: new { controller = "Home", action = "Index" }
  );

I'd like to clean up my Mvc.sitemap file a bit by removing all the urls from the navigation only nodes and trapping some event or overriding some method wherein I can generate the url for the node. The urls for these nodes as you can see can be generated from ancestor and self titles.

I'm not sure if the documentation is addressing this ability.


Solution

  • If you have nodes in your menu that only exist on the client side, then you should not put them into the SiteMap at all. The SiteMap is sort of like a hierarchial database - you shouldn't put any UI logic in the database.

    Instead, you should edit the display templates (or create different templates for different scenarios) in the /Views/Shared/DisplayTemplates/ folder. The purpose of these templates is so you can control the HTML that is output by the HTML helpers.

    You may need to add a custom attribute or two in order to turn on and off specific parts of your display logic, but that can be done among the "real" (server side) navigation locations in your SiteMap.