我想获取Umbraco网站中节点的页数,输出如下:
基本上我正在尝试查看给定网站中有多少活动内容,并想出一种划分工作的方法。有没有合理的方法来获取这些信息?
答案 0 :(得分:1)
这对我有用:
App_Code文件夹中的Descendants.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using umbraco.presentation;
using umbraco.presentation.nodeFactory;
public static class ExtensionMethods
{
public static IEnumerable<Node> AllDescendants(this Node node)
{
foreach (Node child in node.Children)
{
yield return child;
foreach (Node grandChild in child.AllDescendants())
yield return grandChild;
}
}
}
Razor View
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@using umbraco.presentation.nodeFactory;
@{
Layout = "";
}
@functions{
public string CreateSitemap()
{
var temp = "<ul class='collapsibleList'>" + sitemap(-1) + "</ul>" + Environment.NewLine;
return temp;
}
public string sitemap(int nodeID)
{
var rootNode = new umbraco.presentation.nodeFactory.Node(nodeID);
var sitemapstring = "<li>" + rootNode.Name + " (" + rootNode.AllDescendants().Count() + ") <span style='font-size:9px'>" + rootNode.NodeTypeAlias + "</span></li>" + Environment.NewLine;
if (rootNode.Children.Count > 0)
{
sitemapstring += "<ul>" + Environment.NewLine;
sitemapstring = rootNode.Children.Cast<Node>().Aggregate(sitemapstring, (current, node) => current + sitemap(node.Id));
sitemapstring += "</ul>" + Environment.NewLine;
}
return sitemapstring;
}
}
<body>
@Html.Raw(CreateSitemap())
</body>