Search code examples
asp.net-mvcmvcsitemapproviderasp.net-mvc-sitemap

How do I specify an MVC SiteMaps Visibility attribute using Code based attribute?


I am using the latest Nuget package of MVC SiteMap provider. We are making heavy use of code based attributes defining nodes in our site.

E.g. [MvcSiteMapNode(Title = "Examination Types", ParentKey = "LookupTable", Key = "ExaminationTypeIndex")]

We want to make use of a custom visibility provider to hide nodes from SiteMap as per here

However we can't seem to specify node visibility attribute using Code based nodes? Is there anyway to do that. We can only specify a custom visibility provider and we would rather use the visible attribute.


Solution

  • Visibility is a custom attribute, so you need to supply it in the Attributes field in order to use it in conjunction with [MvcSiteMapNodeAttribute]. The only tricky part is that .NET attributes don't support dictionary types, so you need to supply the attributes as an escaped JSON string.

    [MvcSiteMapNode(Title = "Examination Types", ParentKey = "LookupTable", Key = "ExaminationTypeIndex", Attributes = @"{ ""visibility"": ""SiteMapPathHelper,!*"" }")]
    

    NOTE: If you need to supply multiple custom attributes, separate them with a comma.

    Attributes = @"{ ""visibility"": ""SiteMapPathHelper,!*"", ""myCustomAttribute"": true }"