/// <summary>
        /// Checks all attributes on a tag-helper element if any are asp-all-route-data or asp-route-*
        /// Returns null if no route data was found.
        /// </summary>
        /// <param name="attributes"></param>
        /// <returns></returns>
        public static Dictionary <string, string> GetRouteValues(ReadOnlyTagHelperAttributeList attributes)
        {
            bool hasAllRouteData = attributes.TryGetAttribute("asp-all-route-data", out TagHelperAttribute allRouteValues);

            if (hasAllRouteData)
            {
                return(allRouteValues.Value as Dictionary <string, string>);
            }
            else
            {
                // You can write the attributes as ASP-ROUTE-  and the name will be ASP-ROUTE so need to ignore case
                IEnumerable <TagHelperAttribute> routeAttributes = attributes.Where(x => x.Name.StartsWith("asp-route-", StringComparison.OrdinalIgnoreCase));

                if (routeAttributes.Any())
                {
                    Dictionary <string, string> routeValues = new Dictionary <string, string>();

                    foreach (TagHelperAttribute attribute in routeAttributes)
                    {
                        // asp-route- is 10 characters long
                        string parameter = attribute.Name.Substring(10).ToLower();
                        routeValues.Add(parameter, attribute.Value as string);
                    }

                    return(routeValues);
                }
            }

            return(null);
        }
Beispiel #2
0
        public static TagHelperAttribute GetTagHelperAttribute(this ReadOnlyTagHelperAttributeList tagHelperAttribute, string name)
        {
            if (tagHelperAttribute.TryGetAttribute(name, out var attribute))
            {
                return(attribute);
            }

            return(null);
        }
Beispiel #3
0
        private static bool HasMissingAttributes(ReadOnlyTagHelperAttributeList allAttributes, string[] requiredAttributes)
        {
            // Check for all attribute values
            // Perf: Avoid allocating enumerator
            for (var i = 0; i < requiredAttributes.Length; i++)
            {
                if (!allAttributes.TryGetAttribute(requiredAttributes[i], out var attribute))
                {
                    // Missing attribute.
                    return(true);
                }

                if (attribute.Value is string valueAsString && string.IsNullOrEmpty(valueAsString))
                {
                    // Treat attributes with empty values as missing.
                    return(true);
                }
            }

            return(false);
        }