我正在尝试在前端站点上构建辅助导航,但我无法解决问题。在下面的代码中,输出显示类似于:
的内容water -----water -----pollination -----dormancy pollination -----water -----pollination -----dormancy dormancy -----water -----pollination -----dormancy
// Loop through Root Webs in Site Collection
foreach (SPWeb TopLevelWebs in CurrentSite.AllWebs)
{
SPNavigationNodeCollection FirstLevelWebs = TopLevelWebs.Navigation.GlobalNodes[0].Children;
// Loop through each Child of Web
foreach (SPNavigationNode FirstLevelWebChild in FirstLevelWebs)
{
// Skip over pages and only look at webs (or "Area" as it is called in 'Properties')
if (FirstLevelWebChild.Properties["NodeType"].ToString() != "Page")
{
Response.Write(FirstLevelWebChild.Title + "<br />");
SPNavigationNodeCollection SecondLevelWebs = FirstLevelWebChild.Navigation.GlobalNodes[0].Children;
foreach (SPNavigationNode SecondLevelWebChild in SecondLevelWebs)
{
// Skip over pages and only look at webs (or "Area" as it is called in 'Properties')
if (SecondLevelWebChild.Properties["NodeType"].ToString() != "Page")
{
Response.Write("-----" + SecondLevelWebChild.Title + "<br />");
}
}
}
}
}
为什么上面的逻辑对顶级节点下的子网不起作用?我还没有完全理解“GlobalNodes”属性,所以我必须假设这就是问题所在。但我无法提出解决方案。
非常感谢任何帮助。
答案 0 :(得分:1)
我知道这是从很久以前开始的,但是代码中有一个错误,因为谷歌的结果对我来说非常高,我想我应该为遇到它的其他人纠正它。
SPNavigationNodeCollection SecondLevelWebs = FirstLevelWebChild.Navigation.GlobalNodes [0] .Children;
应该是
SPNavigationNodeCollection SecondLevelWebs = FirstLevelWebChild.Children;
通过使用FirstLevelWebChild.Navigation.GlobalNodes,您再次获得根导航而不是您想要获得的子导航。