public static string TitleFor(string action, string controller, string area, string routeName) { MvcSiteMapNode node = FindNode(action, controller, area, routeName); if (node != null) { return(node.Title); } return(String.Empty); }
/// <summary> /// Parses a sitemap xml element into a site node object. /// </summary> private static MvcSiteMapNode NodeFromElement(XElement nodeElement, MvcSiteMapNode parent) { MvcSiteMapNode node = new MvcSiteMapNode(); node.Parent = parent; if (nodeElement.Attribute("controller") != null) { node.Controller = nodeElement.Attribute("controller").Value; } if (nodeElement.Attribute("action") != null) { node.Action = nodeElement.Attribute("action").Value; } if (nodeElement.Attribute("area") != null) { node.Area = nodeElement.Attribute("area").Value; } else { node.Area = String.Empty; } if (nodeElement.Attribute("routeName") != null) { node.RouteName = nodeElement.Attribute("routeName").Value; if (RouteTable.Routes[node.RouteName] == null) { throw new ApplicationException("The route '" + node.RouteName + "' specifed in the sitemap is invalid."); } } if (nodeElement.Attribute("title") != null) { node.Title = nodeElement.Attribute("title").Value; } if (nodeElement.Attribute("description") != null) { node.Description = nodeElement.Attribute("description").Value; } if (nodeElement.Attribute("keywords") != null) { node.Keywords = nodeElement.Attribute("keywords").Value; } if (nodeElement.Attribute("display") != null) { node.Display = (MvcSiteMapDisplay)Enum.Parse(typeof(MvcSiteMapDisplay), nodeElement.Attribute("display").Value); } if (nodeElement.HasElements) { foreach (XElement childElement in nodeElement.Elements("Node")) { node.ChildNodes.Add(NodeFromElement(childElement, node)); } } return(node); }
/// <summary> /// Returns an ordered chain from the top-most parent to the current node. /// </summary> public IEnumerable <MvcSiteMapNode> ParentChain() { List <MvcSiteMapNode> nodesOrdered = new List <MvcSiteMapNode>(); MvcSiteMapNode node = this; while (node != null) { nodesOrdered.Insert(0, node); node = node.Parent; } return(nodesOrdered); }
/// <summary> /// Renders out a ul tag with all site map nodes underneath. /// </summary> public static IHtmlString SiteMapList(this HtmlHelper helper, string action, string controller, string area, string routeName, bool showRootNode, int levels, bool asLinks, bool showDescriptions) { StringBuilder sb = new StringBuilder(); MvcSiteMapNode startNode = MvcSiteMap.FindNode(action, controller, area, routeName); //render the list UrlHelper url = new UrlHelper(helper.ViewContext.RequestContext); if ((startNode.Display & MvcSiteMapDisplay.SiteMap) == MvcSiteMapDisplay.SiteMap) { SiteMapListRenderNode(url, sb, startNode, true, showRootNode, levels, 0, asLinks, showDescriptions); } return(MvcHtmlString.Create(sb.ToString())); }
/// <summary> /// Loads the site map from the xml file. /// </summary> public static void LoadSiteMap(string fileName) { if (File.Exists(fileName)) { XDocument xdoc = XDocument.Load(fileName); if (xdoc.Root != null) { XElement firstNode = xdoc.Root.Element("Node"); if (firstNode != null) { m_RootNode = NodeFromElement(firstNode, null); FileLastModified = File.GetLastWriteTime(fileName); } } } }
public static IEnumerable <MvcSiteMapNode> Descendents(MvcSiteMapNode node) { if (node == null) { throw new ArgumentNullException("node"); } if (node.ChildNodes.Count > 0) { foreach (MvcSiteMapNode child in node.ChildNodes) { yield return(child); foreach (MvcSiteMapNode desc in Descendents(child)) { yield return(desc); } } } }
/// <summary> /// Recursive rendering method for building out a sitemap list. /// </summary> private static void SiteMapListRenderNode(UrlHelper helper, StringBuilder sb, MvcSiteMapNode node, bool isRoot, bool showRootNode, int levels, int currentLevel, bool asLinks, bool showDescriptions) { if (isRoot) { sb.AppendLine("<ul class=\"sitemap-list\">"); } if (isRoot == false || (isRoot && showRootNode)) { if (node == MvcSiteMap.CurrentNode) { sb.Append("<li class=\"selected\">"); } else { sb.Append("<li>"); } if (asLinks) { sb.Append("<a href=\""); if (String.IsNullOrWhiteSpace(node.RouteName)) { sb.Append(helper.Action(node.Action, node.Controller, new { area = node.Area })); } else { sb.Append(helper.RouteUrl(node.RouteName)); } sb.Append("\">"); sb.Append(node.Title); sb.Append("</a>"); } else { sb.Append(node.Title); } if (showDescriptions && String.IsNullOrWhiteSpace(node.Description) == false) { sb.Append("<p>"); sb.Append(node.Description.Trim()); sb.Append("</p>"); } } currentLevel++; if (levels > 0 && levels >= currentLevel) { if (node.ChildNodes.Count > 0) { if (isRoot == false || (isRoot && showRootNode)) { sb.AppendLine(); sb.AppendLine("<ul>"); } foreach (MvcSiteMapNode n in node.ChildNodes) { if ((n.Display & MvcSiteMapDisplay.SiteMap) == MvcSiteMapDisplay.SiteMap) { SiteMapListRenderNode(helper, sb, n, false, false, levels, currentLevel, asLinks, showDescriptions); } } if (isRoot == false || (isRoot && showRootNode)) { sb.AppendLine("</ul>"); } } } sb.AppendLine("</li>"); if (isRoot) { sb.AppendLine("</ul>"); } }
/// <summary> /// Renders out a ul tag with all site map nodes underneath. /// </summary> public static IHtmlString SiteMapList(this HtmlHelper helper, bool showRootNode, int levels, bool asLinks) { MvcSiteMapNode node = MvcSiteMap.CurrentNode; return(SiteMapList(helper, node.Action, node.Controller, node.Area ?? String.Empty, null, showRootNode, levels, asLinks, true)); }
/// <summary> /// Renders out a ul tag with all site map nodes underneath. /// </summary> public static IHtmlString SiteMapList(this HtmlHelper helper) { MvcSiteMapNode node = MvcSiteMap.CurrentNode; return(SiteMapList(helper, node.Action, node.Controller, node.Area ?? String.Empty, null, true, -1, true, true)); }