SiteMap.RootNode 属性

定义

获取一个表示站点的导航结构的顶级页的 SiteMapNode 对象。

public:
 static property System::Web::SiteMapNode ^ RootNode { System::Web::SiteMapNode ^ get(); };
public static System.Web.SiteMapNode RootNode { get; }
static member RootNode : System.Web.SiteMapNode
Public Shared ReadOnly Property RootNode As SiteMapNode

属性值

表示站点的导航结构的顶级页的 SiteMapNode;如果启用了安全修整且不能为当前用户返回该节点,则为 null

例外

未启用站点地图功能。

- 或 -

RootNode 解析为 null,如果启用了安全修整并且根节点对当前用户不可见,就会发生这种情况。

配置中指定的默认提供程序不存在。

只有在低信任或以上设置的情况下运行时,才支持此功能。

示例

下面的代码示例演示如何使用 RootNode 属性检索 SiteMapNode 表示站点根节点的对象,而不考虑页面层次结构中的当前位置。

<script runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
    // Examine the CurrentNode, and navigate the SiteMap relative to it.
    Response.Write(SiteMap.CurrentNode.Title + "<br />");
    Response.Write("<font COLOR='red'>" + SiteMap.CurrentNode.Url + "</font><br />");

    // What nodes are children of the CurrentNode?
    if (SiteMap.CurrentNode.HasChildNodes) {
        IEnumerator childNodesEnumerator = SiteMap.CurrentNode.ChildNodes.GetEnumerator();
        while (childNodesEnumerator.MoveNext()) {
            // Prints the Title of each node.
            Response.Write(childNodesEnumerator.Current.ToString() + "<br />");
        }
    }
    Response.Write("<hr />");

    // Examine the RootNode, and navigate the SiteMap relative to it.
    Response.Write(SiteMap.RootNode.Title + "<br />");
    Response.Write(SiteMap.RootNode.Url + "<br />");

    // What nodes are children of the RootNode?
    if (SiteMap.RootNode.HasChildNodes) {
        IEnumerator rootNodesChildrenEnumerator = SiteMap.RootNode.ChildNodes.GetEnumerator();
        while (rootNodesChildrenEnumerator.MoveNext()) {
            // Prints the Title of each node.
            Response.Write(rootNodesChildrenEnumerator.Current.ToString() + "<br />");
        }
    }
}
</script>
<script runat="server">

Private Sub Page_Load(sender As Object, e As EventArgs)

    ' Examine the CurrentNode, and navigate the SiteMap relative to it.
    Response.Write(SiteMap.CurrentNode.Title & "<br />")
    Response.Write("<font COLOR='red'>" & SiteMap.CurrentNode.Url & "</font><br />")

    ' What nodes are children of the CurrentNode?
    If (SiteMap.CurrentNode.HasChildNodes) Then
        Dim ChildNodesEnumerator As IEnumerator = SiteMap.CurrentNode.ChildNodes.GetEnumerator()
        While (ChildNodesEnumerator.MoveNext())
            ' Prints the Title of each node.
            Response.Write(ChildNodesEnumerator.Current.ToString() & "<br />")
        End While
    End If
    Response.Write("<hr />")

    ' Examine the RootNode, and navigate the SiteMap relative to it.
    Response.Write(SiteMap.RootNode.Title & "<br />")
    Response.Write(SiteMap.RootNode.Url & "<br />")

    ' What nodes are children of the RootNode?
    If (SiteMap.RootNode.HasChildNodes) Then
        Dim RootNodesChildrenEnumerator As IEnumerator = SiteMap.RootNode.ChildNodes.GetEnumerator()
        While (RootNodesChildrenEnumerator.MoveNext())
            ' Prints the Title of each node.
            Response.Write(RootNodesChildrenEnumerator.Current.ToString() & "<br />")
        End While
    End If

End Sub ' Page_Load
</script>

注解

SiteMap 通过从提供程序请求属性来检索 RootNode 该属性。 如果存在提供程序层次结构,则 SiteMap 类检索层次结构中根提供程序的根节点。 若要访问默认提供程序的根节点,请调用 Provider 属性,该属性将检索默认提供程序及其 RootNode 属性。

对象 XmlSiteMapProvider 是 类的默认提供程序 SiteMap ,它仅支持一个 RootNode

如果实现自己的 SiteMapProvider 类并重写抽象 RootNode 属性,则必须提供自己的实现来跟踪 RootNode 实例的属性 SiteMapNode

根据设计,根节点必须对所有用户可见。 如果根节点在启用安全修整时不对所有用户可见,则访问属性 RootNode 可能会导致 InvalidOperationException 异常。

适用于

另请参阅