/// <summary> /// Acquires the preserved route parameters list from a given <see cref="T:IMvcSiteMapNodeAttribute"/> /// </summary> /// <param name="attribute">The attribute.</param> /// <param name="preservedRouteParameters">The preserved route parameters IList to populate.</param> protected virtual void AcquirePreservedRouteParametersFrom(IMvcSiteMapNodeAttribute attribute, IList<string> preservedRouteParameters) { var localParameters = (attribute.PreservedRouteParameters ?? "").Split(new[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries); foreach (var parameter in localParameters) { preservedRouteParameters.Add(parameter); } }
/// <summary> /// Acquires the meta robots values list from a given <see cref="T:IMvcSiteMapNodeAttribute"/> /// </summary> /// <param name="attribute">The attribute.</param> /// <param name="metaRobotsValues">The meta robots values IList to populate.</param> protected virtual void AcquireMetaRobotsValuesFrom(IMvcSiteMapNodeAttribute attribute, IList <string> metaRobotsValues) { if (attribute.MetaRobotsValues != null) { foreach (var value in attribute.MetaRobotsValues) { metaRobotsValues.Add(value); } } }
/// <summary> /// Acquires the roles list from a given <see cref="T:IMvcSiteMapNodeAttribute"/> /// </summary> /// <param name="attribute">The attribute.</param> /// <param name="roles">The roles IList to populate.</param> protected virtual void AcquireRolesFrom(IMvcSiteMapNodeAttribute attribute, IList <string> roles) { if (attribute.Roles != null) { foreach (var role in attribute.Roles) { roles.Add(role); } } }
/// <summary> /// Acquires the attributes from a given <see cref="T:IMvcSiteMapNodeAttribute"/> /// </summary> /// <param name="attribute">The source attribute.</param> /// <param name="attributes">The attribute dictionary to populate.</param> /// <param name="helper">The node helper.</param> /// <returns></returns> protected virtual void AcquireAttributesFrom(IMvcSiteMapNodeAttribute attribute, IDictionary<string, object> attributes, ISiteMapNodeHelper helper) { foreach (var att in attribute.Attributes) { var attributeName = att.Key.ToString(); var attributeValue = att.Value; if (helper.ReservedAttributeNames.IsRegularAttribute(attributeName)) { attributes[attributeName] = attributeValue; } } }
/// <summary> /// Acquires the route values from a given XElement. /// </summary> /// <param name="node">The node.</param> /// <returns></returns> protected virtual void AcquireRouteValuesFrom(IMvcSiteMapNodeAttribute attribute, IRouteValueDictionary routeValues) { foreach (var att in attribute.Attributes) { var attributeName = att.Key.ToString(); var attributeValue = att.Value; if (reservedAttributeNameProvider.IsRouteAttribute(attributeName)) { routeValues[attributeName] = attributeValue; } } }
/// <summary> /// Acquires the attributes from a given <see cref="T:IMvcSiteMapNodeAttribute"/> /// </summary> /// <param name="attribute">The attribute.</param> /// <returns></returns> protected virtual void AcquireAttributesFrom(IMvcSiteMapNodeAttribute attribute, IDictionary <string, object> attributes) { foreach (var att in attribute.Attributes) { var attributeName = att.Key.ToString(); var attributeValue = att.Value; if (reservedAttributeNameProvider.IsRegularAttribute(attributeName)) { attributes[attributeName] = attributeValue; } } }
/// <summary> /// Gets the site map node from MVC site map node attribute. /// </summary> /// <param name="attribute">IMvcSiteMapNodeAttribute to map</param> /// <param name="type">Type.</param> /// <param name="methodInfo">MethodInfo on which the IMvcSiteMapNodeAttribute is applied</param> /// <returns> /// A SiteMapNode which represents the IMvcSiteMapNodeAttribute. /// </returns> protected SiteMapNode GetSiteMapNodeFromMvcSiteMapNodeAttribute(IMvcSiteMapNodeAttribute attribute, Type type, MethodInfo methodInfo) { if (attribute == null) { throw new ArgumentNullException("attribute"); } if (type == null) { throw new ArgumentNullException("type"); } if (methodInfo == null) // try to find Index action { var ms = type.FindMembers(MemberTypes.Method, BindingFlags.Instance | BindingFlags.Public, (mi, o) => mi != null && string.Equals(mi.Name, "Index"), null); foreach (MethodInfo m in ms.OfType<MethodInfo>()) { var pars = m.GetParameters(); if (pars.Length == 0) { methodInfo = m; break; } } } // Determine area (will only work if controller is defined as Assembly.<Area>.Controllers.HomeController) string area = ""; if (!string.IsNullOrEmpty(attribute.AreaName)) { area = attribute.AreaName; } if (string.IsNullOrEmpty(area)) { var parts = type.Namespace.Split('.'); area = parts[parts.Length - 2]; var assemblyParts = type.Assembly.FullName.Split(','); if (type.Namespace == assemblyParts[0] + ".Controllers" || type.Namespace.StartsWith(area)) { // Is in default areaName... area = ""; } } // Determine controller and (index) action string controller = type.Name.Substring(0, type.Name.IndexOf("Controller")); string action = (methodInfo != null ? methodInfo.Name : null) ?? "Index"; if (methodInfo != null) // handle custom action name { var actionNameAttribute = methodInfo.GetCustomAttributes(typeof(ActionNameAttribute), true).FirstOrDefault() as ActionNameAttribute; if (actionNameAttribute != null) { action = actionNameAttribute.Name; } } // Generate key for node string key = NodeKeyGenerator.GenerateKey( null, attribute.Key, attribute.Url, attribute.Title, area, controller, action, attribute.Clickable); // Handle title and description globalization var explicitResourceKeys = new NameValueCollection(); var title = attribute.Title; var description = attribute.Description; HandleResourceAttribute("title", ref title, ref explicitResourceKeys); HandleResourceAttribute("description", ref description, ref explicitResourceKeys); // Create a new SiteMap node, setting the key and url var siteMapNode = CreateSiteMapNode(key, explicitResourceKeys, null); // Set the properties on siteMapNode. siteMapNode.Title = title; siteMapNode.Description = description; siteMapNode.Roles = attribute.Roles; if (!string.IsNullOrEmpty(attribute.Route)) { siteMapNode["route"] = attribute.Route; } siteMapNode["area"] = area; siteMapNode["controller"] = controller; siteMapNode["action"] = action; siteMapNode["dynamicNodeProvider"] = attribute.DynamicNodeProvider; siteMapNode["urlResolver"] = attribute.UrlResolver; siteMapNode["visibilityProvider"] = attribute.VisibilityProvider; siteMapNode.LastModifiedDate = attribute.LastModifiedDate; siteMapNode.ChangeFrequency = attribute.ChangeFrequency; siteMapNode.UpdatePriority = attribute.UpdatePriority; siteMapNode.TargetFrame = attribute.TargetFrame; siteMapNode.ImageUrl = attribute.ImageUrl; siteMapNode.PreservedRouteParameters = attribute.PreservedRouteParameters; if (!string.IsNullOrEmpty(attribute.Url)) siteMapNode.Url = attribute.Url; siteMapNode.ResourceKey = attribute.ResourceKey; // Create a route data dictionary IDictionary<string, object> routeValues = new Dictionary<string, object>(); routeValues.Add("area", area); routeValues.Add("controller", controller); routeValues.Add("action", action); // Add route values to sitemap node siteMapNode.RouteValues = routeValues; // Add defaults for SiteMapNodeUrlResolver if (siteMapNode.UrlResolver == null) { siteMapNode.UrlResolver = this.SiteMapNodeUrlResolver; } // Add defaults for SiteMapNodeVisibilityProvider if (siteMapNode.VisibilityProvider == null) { siteMapNode.VisibilityProvider = this.SiteMapNodeVisibilityProvider; } // Clickable? siteMapNode.Clickable = attribute.Clickable; return siteMapNode; }
/// <summary> /// Gets the site map node from MVC site map node attribute. /// </summary> /// <param name="siteMap">The site map.</param> /// <param name="attribute">The attribute.</param> /// <param name="type">The type.</param> /// <param name="methodInfo">The method info.</param> /// <returns></returns> protected virtual ISiteMapNode GetSiteMapNodeFromMvcSiteMapNodeAttribute(ISiteMap siteMap, IMvcSiteMapNodeAttribute attribute, Type type, MethodInfo methodInfo) { if (attribute == null) { throw new ArgumentNullException("attribute"); } if (type == null) { throw new ArgumentNullException("type"); } if (!string.IsNullOrEmpty(attribute.SiteMapCacheKey)) { // Return null if the attribute doesn't apply to this cache key if (!this.SiteMapCacheKey.Equals(attribute.SiteMapCacheKey)) { return null; } } if (methodInfo == null) // try to find Index action { var ms = type.FindMembers(MemberTypes.Method, BindingFlags.Instance | BindingFlags.Public, (mi, o) => mi != null && string.Equals(mi.Name, "Index"), null); foreach (MethodInfo m in ms.OfType<MethodInfo>()) { var pars = m.GetParameters(); if (pars.Length == 0) { methodInfo = m; break; } } } string area = ""; if (!string.IsNullOrEmpty(attribute.AreaName)) { area = attribute.AreaName; } if (string.IsNullOrEmpty(area) && !string.IsNullOrEmpty(attribute.Area)) { area = attribute.Area; } // Determine area (will only work if controller is defined as [<Anything>.]Areas.<Area>.Controllers.<AnyController>) if (string.IsNullOrEmpty(area)) { var m = Regex.Match(type.Namespace, @"(?:[^\.]+\.|\s+|^)Areas\.(?<areaName>[^\.]+)\.Controllers"); if (m.Success) { area = m.Groups["areaName"].Value; } } // Determine controller and (index) action string controller = type.Name.Substring(0, type.Name.IndexOf("Controller")); string action = (methodInfo != null ? methodInfo.Name : null) ?? "Index"; if (methodInfo != null) { // handle ActionNameAttribute var actionNameAttribute = methodInfo.GetCustomAttributes(typeof(ActionNameAttribute), true).FirstOrDefault() as ActionNameAttribute; if (actionNameAttribute != null) { action = actionNameAttribute.Name; } } string httpMethod = string.IsNullOrEmpty(attribute.HttpMethod) ? HttpVerbs.Get.ToString().ToUpperInvariant() : attribute.HttpMethod.ToUpperInvariant(); // Handle title var title = attribute.Title; // Handle implicit resources var implicitResourceKey = attribute.ResourceKey; // Generate key for node string key = nodeKeyGenerator.GenerateKey( null, attribute.Key, "", title, area, controller, action, httpMethod, attribute.Clickable); var siteMapNode = siteMapNodeFactory.Create(siteMap, key, implicitResourceKey); // Assign defaults siteMapNode.Title = title; siteMapNode.Description = attribute.Description; siteMapNode.Attributes.AddRange(attribute.Attributes, false); siteMapNode.Roles.AddRange(attribute.Roles); siteMapNode.Clickable = attribute.Clickable; siteMapNode.VisibilityProvider = attribute.VisibilityProvider; siteMapNode.DynamicNodeProvider = attribute.DynamicNodeProvider; siteMapNode.ImageUrl = attribute.ImageUrl; siteMapNode.ImageUrlProtocol = attribute.ImageUrlProtocol; siteMapNode.ImageUrlHostName = attribute.ImageUrlHostName; siteMapNode.TargetFrame = attribute.TargetFrame; siteMapNode.HttpMethod = httpMethod; if (!string.IsNullOrEmpty(attribute.Url)) siteMapNode.Url = attribute.Url; siteMapNode.CacheResolvedUrl = attribute.CacheResolvedUrl; siteMapNode.IncludeAmbientValuesInUrl = attribute.IncludeAmbientValuesInUrl; siteMapNode.Protocol = attribute.Protocol; siteMapNode.HostName = attribute.HostName; siteMapNode.CanonicalKey = attribute.CanonicalKey; siteMapNode.CanonicalUrl = attribute.CanonicalUrl; siteMapNode.CanonicalUrlProtocol = attribute.CanonicalUrlProtocol; siteMapNode.CanonicalUrlHostName = attribute.CanonicalUrlHostName; siteMapNode.MetaRobotsValues.AddRange(attribute.MetaRobotsValues); siteMapNode.LastModifiedDate = string.IsNullOrEmpty(attribute.LastModifiedDate) ? DateTime.MinValue : DateTime.Parse(attribute.LastModifiedDate, CultureInfo.InvariantCulture); siteMapNode.ChangeFrequency = attribute.ChangeFrequency; siteMapNode.UpdatePriority = attribute.UpdatePriority; siteMapNode.Order = attribute.Order; // Handle route details siteMapNode.Route = attribute.Route; siteMapNode.RouteValues.AddRange(attribute.Attributes, false); siteMapNode.PreservedRouteParameters.AddRange(attribute.PreservedRouteParameters, new[] { ',', ';' }); siteMapNode.UrlResolver = attribute.UrlResolver; // Specified area, controller and action properties will override any // provided in the attributes collection. if (!string.IsNullOrEmpty(area)) siteMapNode.RouteValues.Add("area", area); if (!string.IsNullOrEmpty(controller)) siteMapNode.RouteValues.Add("controller", controller); if (!string.IsNullOrEmpty(action)) siteMapNode.RouteValues.Add("action", action); return siteMapNode; }
/// <summary> /// Acquires the meta robots values list from a given <see cref="T:IMvcSiteMapNodeAttribute"/> /// </summary> /// <param name="attribute">The attribute.</param> /// <param name="metaRobotsValues">The meta robots values IList to populate.</param> protected virtual void AcquireMetaRobotsValuesFrom(IMvcSiteMapNodeAttribute attribute, IList<string> metaRobotsValues) { if (attribute.MetaRobotsValues != null) { foreach (var value in attribute.MetaRobotsValues) { metaRobotsValues.Add(value); } } }
/// <summary> /// Gets the site map node from MVC site map node attribute. /// </summary> /// <param name="attribute">The attribute.</param> /// <param name="type">The type.</param> /// <param name="methodInfo">The method info.</param> /// <param name="helper">The node helper.</param> /// <returns></returns> protected virtual ISiteMapNodeToParentRelation GetSiteMapNodeFromMvcSiteMapNodeAttribute( IMvcSiteMapNodeAttribute attribute, Type type, MethodInfo methodInfo, ISiteMapNodeHelper helper) { if (attribute == null) { throw new ArgumentNullException("attribute"); } if (type == null) { throw new ArgumentNullException("type"); } if (!String.IsNullOrEmpty(attribute.SiteMapCacheKey)) { // Return null if the attribute doesn't apply to this cache key if (!helper.SiteMapCacheKey.Equals(attribute.SiteMapCacheKey)) { return null; } } if (methodInfo == null) // try to find Index action { var ms = type.FindMembers(MemberTypes.Method, BindingFlags.Instance | BindingFlags.Public, (mi, o) => mi != null && string.Equals(mi.Name, "Index"), null); foreach (MethodInfo m in ms.OfType<MethodInfo>()) { var pars = m.GetParameters(); if (pars.Length == 0) { methodInfo = m; break; } } } string area = ""; if (!String.IsNullOrEmpty(attribute.AreaName)) { area = attribute.AreaName; } if (String.IsNullOrEmpty(area) && !String.IsNullOrEmpty(attribute.Area)) { area = attribute.Area; } // Determine area (will only work if controller is defined as [<Anything>.]Areas.<Area>.Controllers.<AnyController>) if (String.IsNullOrEmpty(area)) { var m = Regex.Match(type.Namespace, @"(?:[^\.]+\.|\s+|^)Areas\.(?<areaName>[^\.]+)\.Controllers"); if (m.Success) { area = m.Groups["areaName"].Value; } } // Determine controller and (index) action string controller = type.Name.Substring(0, type.Name.IndexOf("Controller")); string action = (methodInfo != null ? methodInfo.Name : null) ?? "Index"; if (methodInfo != null) { // handle ActionNameAttribute var actionNameAttribute = methodInfo.GetCustomAttributes(typeof(ActionNameAttribute), true).FirstOrDefault() as ActionNameAttribute; if (actionNameAttribute != null) { action = actionNameAttribute.Name; } } string httpMethod = String.IsNullOrEmpty(attribute.HttpMethod) ? HttpVerbs.Get.ToString().ToUpperInvariant() : attribute.HttpMethod.ToUpperInvariant(); // Handle title and description var title = attribute.Title; var description = String.IsNullOrEmpty(attribute.Description) ? title : attribute.Description; // Handle implicit resources var implicitResourceKey = attribute.ResourceKey; // Generate key for node string key = helper.CreateNodeKey( attribute.ParentKey, attribute.Key, attribute.Url, title, area, controller, action, httpMethod, attribute.Clickable); var nodeParentMap = helper.CreateNode(key, attribute.ParentKey, SourceName, implicitResourceKey); var node = nodeParentMap.Node; // Assign defaults node.Title = title; node.Description = description; AcquireAttributesFrom(attribute, node.Attributes, helper); AcquireRolesFrom(attribute, node.Roles); node.Clickable = attribute.Clickable; node.VisibilityProvider = attribute.VisibilityProvider; node.DynamicNodeProvider = attribute.DynamicNodeProvider; node.ImageUrl = attribute.ImageUrl; node.TargetFrame = attribute.TargetFrame; node.HttpMethod = httpMethod; if (!string.IsNullOrEmpty(attribute.Url)) node.Url = attribute.Url; node.CacheResolvedUrl = attribute.CacheResolvedUrl; node.CanonicalUrl = attribute.CanonicalUrl; node.CanonicalKey = attribute.CanonicalKey; AcquireMetaRobotsValuesFrom(attribute, node.MetaRobotsValues); node.LastModifiedDate = attribute.LastModifiedDate; node.ChangeFrequency = attribute.ChangeFrequency; node.UpdatePriority = attribute.UpdatePriority; node.Order = attribute.Order; // Handle route details node.Route = attribute.Route; AcquireRouteValuesFrom(attribute, node.RouteValues, helper); AcquirePreservedRouteParametersFrom(attribute, node.PreservedRouteParameters); node.UrlResolver = attribute.UrlResolver; // Specified area, controller and action properties will override any // provided in the attributes collection. if (!string.IsNullOrEmpty(area)) node.RouteValues.Add("area", area); if (!string.IsNullOrEmpty(controller)) node.RouteValues.Add("controller", controller); if (!string.IsNullOrEmpty(action)) node.RouteValues.Add("action", action); // Handle MVC details // Add defaults for area if (!node.RouteValues.ContainsKey("area")) { node.RouteValues.Add("area", ""); } return nodeParentMap; }
/// <summary> /// Acquires the roles list from a given <see cref="T:IMvcSiteMapNodeAttribute"/> /// </summary> /// <param name="attribute">The attribute.</param> /// <param name="roles">The roles IList to populate.</param> protected virtual void AcquireRolesFrom(IMvcSiteMapNodeAttribute attribute, IList<string> roles) { if (attribute.Roles != null) { foreach (var role in attribute.Roles) { roles.Add(role); } } }
/// <summary> /// Gets the site map node from MVC site map node attribute. /// </summary> /// <param name="siteMap">The site map.</param> /// <param name="attribute">The attribute.</param> /// <param name="type">The type.</param> /// <param name="methodInfo">The method info.</param> /// <returns></returns> protected virtual ISiteMapNode GetSiteMapNodeFromMvcSiteMapNodeAttribute(ISiteMap siteMap, IMvcSiteMapNodeAttribute attribute, Type type, MethodInfo methodInfo) { if (attribute == null) { throw new ArgumentNullException("attribute"); } if (type == null) { throw new ArgumentNullException("type"); } if (!String.IsNullOrEmpty(attribute.SiteMapCacheKey)) { // Return null if the attribute doesn't apply to this cache key if (!this.SiteMapCacheKey.Equals(attribute.SiteMapCacheKey)) { return null; } } if (methodInfo == null) // try to find Index action { var ms = type.FindMembers(MemberTypes.Method, BindingFlags.Instance | BindingFlags.Public, (mi, o) => mi != null && string.Equals(mi.Name, "Index"), null); foreach (MethodInfo m in ms.OfType<MethodInfo>()) { var pars = m.GetParameters(); if (pars.Length == 0) { methodInfo = m; break; } } } // Determine area (will only work if controller is defined as Assembly.<Area>.Controllers.HomeController) string area = ""; if (!string.IsNullOrEmpty(attribute.AreaName)) { area = attribute.AreaName; } if (string.IsNullOrEmpty(area)) { var parts = type.Namespace.Split('.'); area = parts[parts.Length - 2]; var assemblyParts = type.Assembly.FullName.Split(','); if (type.Namespace == assemblyParts[0] + ".Controllers" || type.Namespace.StartsWith(area)) { // Is in default areaName... area = ""; } } // Determine controller and (index) action string controller = type.Name.Substring(0, type.Name.IndexOf("Controller")); string action = (methodInfo != null ? methodInfo.Name : null) ?? "Index"; string httpMethod = "*"; if (methodInfo != null) { // handle ActionNameAttribute var actionNameAttribute = methodInfo.GetCustomAttributes(typeof(ActionNameAttribute), true).FirstOrDefault() as ActionNameAttribute; if (actionNameAttribute != null) { action = actionNameAttribute.Name; } // handle AcceptVerbsAttribute var acceptVerbsAttribute = methodInfo.GetCustomAttributes(typeof(AcceptVerbsAttribute), true).FirstOrDefault() as AcceptVerbsAttribute; if (acceptVerbsAttribute != null) { httpMethod = string.Join(",", acceptVerbsAttribute.Verbs.ToArray()); } } // Handle title and description var title = attribute.Title; var description = String.IsNullOrEmpty(attribute.Description) ? title : attribute.Description; // Handle implicit resources var implicitResourceKey = attribute.ResourceKey; // Generate key for node string key = nodeKeyGenerator.GenerateKey( null, attribute.Key, "", title, area, controller, action, httpMethod, attribute.Clickable); var siteMapNode = siteMapNodeFactory.Create(siteMap, key, implicitResourceKey); // Assign defaults siteMapNode.Title = title; siteMapNode.Description = description; AcquireAttributesFrom(attribute, siteMapNode.Attributes); AcquireRolesFrom(attribute, siteMapNode.Roles); siteMapNode.Clickable = attribute.Clickable; siteMapNode.VisibilityProvider = attribute.VisibilityProvider; siteMapNode.DynamicNodeProvider = attribute.DynamicNodeProvider; siteMapNode.ImageUrl = attribute.ImageUrl; siteMapNode.TargetFrame = attribute.TargetFrame; siteMapNode.HttpMethod = httpMethod; if (!string.IsNullOrEmpty(attribute.Url)) siteMapNode.Url = attribute.Url; siteMapNode.CacheResolvedUrl = attribute.CacheResolvedUrl; siteMapNode.CanonicalUrl = attribute.CanonicalUrl; siteMapNode.CanonicalKey = attribute.CanonicalKey; AcquireMetaRobotsValuesFrom(attribute, siteMapNode.MetaRobotsValues); siteMapNode.LastModifiedDate = attribute.LastModifiedDate; siteMapNode.ChangeFrequency = attribute.ChangeFrequency; siteMapNode.UpdatePriority = attribute.UpdatePriority; // Handle route details siteMapNode.Route = attribute.Route; AcquireRouteValuesFrom(attribute, siteMapNode.RouteValues); AcquirePreservedRouteParametersFrom(attribute, siteMapNode.PreservedRouteParameters); siteMapNode.UrlResolver = attribute.UrlResolver; // Specified area, controller and action properties will override any // provided in the attributes collection. if (!string.IsNullOrEmpty(area)) siteMapNode.RouteValues.Add("area", area); if (!string.IsNullOrEmpty(controller)) siteMapNode.RouteValues.Add("controller", controller); if (!string.IsNullOrEmpty(action)) siteMapNode.RouteValues.Add("action", action); // Handle MVC details // Add defaults for area if (!siteMapNode.RouteValues.ContainsKey("area")) { siteMapNode.RouteValues.Add("area", ""); } return siteMapNode; }
/// <summary> /// Gets the site map node from MVC site map node attribute. /// </summary> /// <param name="siteMap">The site map.</param> /// <param name="attribute">The attribute.</param> /// <param name="type">The type.</param> /// <param name="methodInfo">The method info.</param> /// <returns></returns> protected virtual ISiteMapNode GetSiteMapNodeFromMvcSiteMapNodeAttribute(ISiteMap siteMap, IMvcSiteMapNodeAttribute attribute, Type type, MethodInfo methodInfo) { if (attribute == null) { throw new ArgumentNullException("attribute"); } if (type == null) { throw new ArgumentNullException("type"); } if (!String.IsNullOrEmpty(attribute.SiteMapCacheKey)) { // Return null if the attribute doesn't apply to this cache key if (!this.SiteMapCacheKey.Equals(attribute.SiteMapCacheKey)) { return(null); } } if (methodInfo == null) // try to find Index action { var ms = type.FindMembers(MemberTypes.Method, BindingFlags.Instance | BindingFlags.Public, (mi, o) => mi != null && string.Equals(mi.Name, "Index"), null); foreach (MethodInfo m in ms.OfType <MethodInfo>()) { var pars = m.GetParameters(); if (pars.Length == 0) { methodInfo = m; break; } } } string area = ""; if (!string.IsNullOrEmpty(attribute.AreaName)) { area = attribute.AreaName; } if (String.IsNullOrEmpty(area) && !String.IsNullOrEmpty(attribute.Area)) { area = attribute.Area; } // Determine area (will only work if controller is defined as [<Anything>.]Areas.<Area>.Controllers.<AnyController>) if (string.IsNullOrEmpty(area)) { var m = Regex.Match(type.Namespace, @"(?:[^\.]+\.|\s+|^)Areas\.(?<areaName>[^\.]+)\.Controllers"); if (m.Success) { area = m.Groups["areaName"].Value; } } // Determine controller and (index) action string controller = type.Name.Substring(0, type.Name.IndexOf("Controller")); string action = (methodInfo != null ? methodInfo.Name : null) ?? "Index"; if (methodInfo != null) { // handle ActionNameAttribute var actionNameAttribute = methodInfo.GetCustomAttributes(typeof(ActionNameAttribute), true).FirstOrDefault() as ActionNameAttribute; if (actionNameAttribute != null) { action = actionNameAttribute.Name; } } string httpMethod = String.IsNullOrEmpty(attribute.HttpMethod) ? HttpVerbs.Get.ToString().ToUpperInvariant() : attribute.HttpMethod.ToUpperInvariant(); // Handle title and description var title = attribute.Title; var description = String.IsNullOrEmpty(attribute.Description) ? title : attribute.Description; // Handle implicit resources var implicitResourceKey = attribute.ResourceKey; // Generate key for node string key = nodeKeyGenerator.GenerateKey( null, attribute.Key, "", title, area, controller, action, httpMethod, attribute.Clickable); var siteMapNode = siteMapNodeFactory.Create(siteMap, key, implicitResourceKey); // Assign defaults siteMapNode.Title = title; siteMapNode.Description = description; AcquireAttributesFrom(attribute, siteMapNode.Attributes); AcquireRolesFrom(attribute, siteMapNode.Roles); siteMapNode.Clickable = attribute.Clickable; siteMapNode.VisibilityProvider = attribute.VisibilityProvider; siteMapNode.DynamicNodeProvider = attribute.DynamicNodeProvider; siteMapNode.ImageUrl = attribute.ImageUrl; siteMapNode.TargetFrame = attribute.TargetFrame; siteMapNode.HttpMethod = httpMethod; if (!string.IsNullOrEmpty(attribute.Url)) { siteMapNode.Url = attribute.Url; } siteMapNode.CacheResolvedUrl = attribute.CacheResolvedUrl; siteMapNode.CanonicalUrl = attribute.CanonicalUrl; siteMapNode.CanonicalKey = attribute.CanonicalKey; AcquireMetaRobotsValuesFrom(attribute, siteMapNode.MetaRobotsValues); siteMapNode.LastModifiedDate = attribute.LastModifiedDate; siteMapNode.ChangeFrequency = attribute.ChangeFrequency; siteMapNode.UpdatePriority = attribute.UpdatePriority; siteMapNode.Order = attribute.Order; // Handle route details siteMapNode.Route = attribute.Route; AcquireRouteValuesFrom(attribute, siteMapNode.RouteValues); AcquirePreservedRouteParametersFrom(attribute, siteMapNode.PreservedRouteParameters); siteMapNode.UrlResolver = attribute.UrlResolver; // Specified area, controller and action properties will override any // provided in the attributes collection. if (!string.IsNullOrEmpty(area)) { siteMapNode.RouteValues.Add("area", area); } if (!string.IsNullOrEmpty(controller)) { siteMapNode.RouteValues.Add("controller", controller); } if (!string.IsNullOrEmpty(action)) { siteMapNode.RouteValues.Add("action", action); } // Handle MVC details // Add defaults for area if (!siteMapNode.RouteValues.ContainsKey("area")) { siteMapNode.RouteValues.Add("area", ""); } return(siteMapNode); }
/// <summary> /// Gets the site map node from MVC site map node attribute. /// </summary> /// <param name="attribute">The attribute.</param> /// <param name="type">The type.</param> /// <param name="methodInfo">The method info.</param> /// <param name="helper">The node helper.</param> /// <returns></returns> protected virtual ISiteMapNodeToParentRelation GetSiteMapNodeFromMvcSiteMapNodeAttribute( IMvcSiteMapNodeAttribute attribute, Type type, MethodInfo methodInfo, ISiteMapNodeHelper helper) { if (attribute == null) { throw new ArgumentNullException("attribute"); } if (type == null) { throw new ArgumentNullException("type"); } if (!string.IsNullOrEmpty(attribute.SiteMapCacheKey)) { // Return null if the attribute doesn't apply to this cache key if (!helper.SiteMapCacheKey.Equals(attribute.SiteMapCacheKey)) { return(null); } } if (methodInfo == null) // try to find Index action { var ms = type.FindMembers(MemberTypes.Method, BindingFlags.Instance | BindingFlags.Public, (mi, o) => mi != null && string.Equals(mi.Name, "Index"), null); foreach (MethodInfo m in ms.OfType <MethodInfo>()) { var pars = m.GetParameters(); if (pars.Length == 0) { methodInfo = m; break; } } } string area = ""; if (!string.IsNullOrEmpty(attribute.AreaName)) { area = attribute.AreaName; } if (string.IsNullOrEmpty(area) && !string.IsNullOrEmpty(attribute.Area)) { area = attribute.Area; } // Determine area (will only work if controller is defined as [<Anything>.]Areas.<Area>.Controllers.<AnyController>) if (string.IsNullOrEmpty(area)) { var m = Regex.Match(type.Namespace, @"(?:[^\.]+\.|\s+|^)Areas\.(?<areaName>[^\.]+)\.Controllers"); if (m.Success) { area = m.Groups["areaName"].Value; } } // Determine controller and (index) action string controller = type.Name.Substring(0, type.Name.IndexOf("Controller")); string action = (methodInfo != null ? methodInfo.Name : null) ?? "Index"; if (methodInfo != null) { // handle ActionNameAttribute var actionNameAttribute = methodInfo.GetCustomAttributes(typeof(ActionNameAttribute), true).FirstOrDefault() as ActionNameAttribute; if (actionNameAttribute != null) { action = actionNameAttribute.Name; } } string httpMethod = (string.IsNullOrEmpty(attribute.HttpMethod) ? HttpVerbs.Get.ToString() : attribute.HttpMethod).ToUpper(); // Handle title var title = attribute.Title; // Handle implicit resources var implicitResourceKey = attribute.ResourceKey; // Generate key for node string key = helper.CreateNodeKey( attribute.ParentKey, attribute.Key, attribute.Url, title, area, controller, action, httpMethod, attribute.Clickable); var nodeParentMap = helper.CreateNode(key, attribute.ParentKey, SourceName, implicitResourceKey); var node = nodeParentMap.Node; // Assign defaults node.Site = attribute.Site; node.Title = title; node.Description = attribute.Description; node.Attributes.AddRange(attribute.Attributes, false); node.Roles.AddRange(attribute.Roles); node.Clickable = attribute.Clickable; node.VisibilityProvider = attribute.VisibilityProvider; node.DynamicNodeProvider = attribute.DynamicNodeProvider; node.ImageUrl = attribute.ImageUrl; node.ImageUrlProtocol = attribute.ImageUrlProtocol; node.ImageUrlHostName = attribute.ImageUrlHostName; node.TargetFrame = attribute.TargetFrame; node.HttpMethod = httpMethod; if (!string.IsNullOrEmpty(attribute.Url)) { node.Url = attribute.Url; } node.CacheResolvedUrl = attribute.CacheResolvedUrl; node.IncludeAmbientValuesInUrl = attribute.IncludeAmbientValuesInUrl; node.Protocol = attribute.Protocol; node.HostName = attribute.HostName; node.CanonicalKey = attribute.CanonicalKey; node.CanonicalUrl = attribute.CanonicalUrl; node.CanonicalUrlSeo = attribute.CanonicalUrlSeo; node.CanonicalUrlProtocol = attribute.CanonicalUrlProtocol; node.CanonicalUrlHostName = attribute.CanonicalUrlHostName; node.MetaRobotsValues.AddRange(attribute.MetaRobotsValues); node.Include = attribute.Include; node.Exclude = attribute.Exclude; node.LastModifiedDate = string.IsNullOrEmpty(attribute.LastModifiedDate) ? DateTime.MinValue : DateTime.Parse(attribute.LastModifiedDate); node.ExpirationDate = string.IsNullOrEmpty(attribute.ExpirationDate) ? DateTime.Today.AddYears(2) : DateTime.Parse(attribute.ExpirationDate); node.ChangeFrequency = attribute.ChangeFrequency; node.UpdatePriority = attribute.UpdatePriority; node.Order = attribute.Order; node.IsVideo = attribute.IsVideo; node.ContentLocationUrl = attribute.ContentLocationUrl; node.PlayerLocationUrl = attribute.PlayerLocationUrl; node.PlayerAllowEmbed = attribute.PlayerAllowEmbed; node.PlayerAutoplay = attribute.PlayerAutoplay; node.VideoDuration = attribute.VideoDuration; node.ViewCount = attribute.ViewCount; node.VideoRating = attribute.VideoRating; node.FamilyFriendly = attribute.FamilyFriendly; node.GalleryLocation = attribute.GalleryLocation; node.GalleryTitle = attribute.GalleryTitle; node.RequiresSubscription = attribute.RequiresSubscription; node.VideoUploader = attribute.VideoUploader; node.VideoUploaderUrl = attribute.VideoUploaderUrl; node.VideoLive = attribute.VideoLive; // Handle route details node.Route = attribute.Route; node.RouteValues.AddRange(attribute.Attributes, false); node.PreservedRouteParameters.AddRange(attribute.PreservedRouteParameters, new[] { ',', ';' }); node.UrlResolver = attribute.UrlResolver; // Specified area, controller and action properties will override any // provided in the attributes collection. if (!string.IsNullOrEmpty(area)) { node.RouteValues.Add("area", area); } if (!string.IsNullOrEmpty(controller)) { node.RouteValues.Add("controller", controller); } if (!string.IsNullOrEmpty(action)) { node.RouteValues.Add("action", action); } return(nodeParentMap); }