语言

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 异常。

适用于

另请参阅