Html 5 Attributes

Jul 16, 2011 at 4:01 PM

Hi,
I'm using the MvcSiteMapProvider for quite some time and until now when using custom attributes it was no problem to add them to the attributesToIgnore setting in the web.config. But now with the convenience and upcoming usage of Html 5 attributes, this can easily be forgotten or tedious to do when one needs serveral "data-" attributes in one node. I was wondering if there is a way to automatically ignore those Html 5 data- attributes from code so they won't be appended as querystring and therefor not manually add them to the web.config file. For example, currently I've overwritten the MvcSiteMapNode and check for htmlDataAttributes to create a dictionary and if needed translate some of the the values and then feed this dictionary to my RenderNode method, so when an attribute is detected that starts with "data-" it gets filtered out and added to my dictionary which I can merge with an actionLink or other helper tag.
It would be nice if from code could be checked for an html 5 data- attribute and if found execute a method like If(attribute.IsHtmlDataAttribute) then IgnoreAttributeForQueryString ... or something in those lines.

Thanks

Jul 23, 2011 at 12:42 AM

Hi Again,

For now I fixed my problem by inheriting from de DefaultSiteMapProvider and overwriting the AttributesToRouteValues. This way I don't have to worry about adding those html "data-" attributes to the attributesToIgnore setting in the web.config. I don't know if this is the best solution but it works for me. So if anyone has the same problem, below the code.

Public Class MySiteMapProvider
Inherits MvcSiteMapProvider.DefaultSiteMapProvider

Private attributesToIgnore as string()

Public Overrides Sub Initialize(ByVal name As String, ByVal attributes As System.Collections.Specialized.NameValueCollection)

            Dim delimiter As Char() = New Char() {",", ";"}
            Dim defaultAttributes = attributes("defaultAttributes") 'added to the web.config as setting e.g.: defaultAttributes="title, description, ..."
            Dim ignorableAttributes = attributes("attributesToIgnore")

            If Not (String.IsNullOrEmpty(ignorableAttributes)) Then
                attributesToIgnore = String.Concat(defaultAttributes, delimiter, ignorableAttributes).Split(delimiter)
                attributes.Remove("attributesToIgnore") 'Remove to avoid double check in the base class
            Else
                attributesToIgnore = (defaultAttributes).Split(delimiter)
            End If

            MyBase.Initialize(name, attributes)
        End Sub

Protected Overrides Sub AttributesToRouteValues(node As System.Xml.Linq.XElement, siteMapNode As MvcSiteMapProvider.MvcSiteMapNode, routeValues As System.Collections.Generic.IDictionary(Of String, Object))

            For Each attribute As XAttribute In node.Attributes
                Dim attributeName = attribute.Name.ToString()
                Dim attributeValue = attribute.Value()

                If (attributeName <> "title" And attributeName <> "description") Then
                    siteMapNode(attributeName) = attributeValue
                End If

                If Not attributesToIgnore.Contains(attributeName) Xor attributeName.IsHtmlDataAttribute() Then ' IsHtmlDataAttribute is a custom extension but basically just checks if an attribute starts with "data-"
                    routeValues.Add(attributeName, attributeValue)
                End If

                If (attributeName.Equals("roles")) Then
                    siteMapNode.Roles = attribute.Value.Split(New Char() {","c}, StringSplitOptions.RemoveEmptyEntries)
                End If
            Next
            'MyBase.AttributesToRouteValues(node, siteMapNode, routeValues)
        End Sub
This solution can probably be improved, but as stated above, this solved my problem.

@maartenba thanks again for the great work on this sitemapprovider.

David.

Coordinator
Jul 25, 2011 at 11:12 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Coordinator
Jul 25, 2011 at 11:14 AM

There's an official fix in the 3.1.0 branch sources, feel free to verify :-)