更新:2007 年 11 月
可以使用导航控件在网页中添加站点导航,这只需编写很少或无需编写代码,但也可以通过编程方式使用站点导航。当 Web 应用程序运行时,ASP.NET 创建一个反映站点地图结构的 SiteMap 对象。SiteMap 对象则依次公开包含站点地图中每个节点的属性的 SiteMapNode 对象的集合。
导航控件(例如 SiteMapPath 控件)使用 SiteMap 和 SiteMapNode 对象来自动呈现适当的链接。
您可以在自己的代码中使用 SiteMap 和 SiteMapNode 对象来创建自定义导航。
示例
下面的代码示例演示如何显示当前页的所有子节点的标题(当前页需在站点地图文件中列出)。如果当前页没有在站点地图文件中列出,使用 SiteMap 对象的第一行代码将引发 NullReferenceException 异常。
<%@ Page language="VB" AutoEventWireup="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
  Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    Try
      Dim LabelText As String = ""
      ' Displays the title of the current node.
      Label_CurrentNode.Text = SiteMap.CurrentNode.Title
      ' Determines if the current node has child nodes.
      If (SiteMap.CurrentNode.HasChildNodes) Then
        For Each ChildNodesEnumerator As SiteMapNode In SiteMap.CurrentNode.ChildNodes
          ' Displays the title of each node.
          LabelText = LabelText & ChildNodesEnumerator.Title & "<br />"
        Next
      Else
        LabelText = LabelText & "No child nodes."
      End If
      Label_ChildNodes.Text = LabelText
    Catch ex As NullReferenceException
      Label_CurrentNode.Text = "The current file is not in the site map."
    Catch ex As Exception
      Label_CurrentNode.Text = "Generic exception: " & e.ToString()
    End Try
  End Sub ' Page_Load
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head>
    <title>Enumerating Child Site Map Nodes</title>
  </head>
  <body>
    <form id="Form1" method="post" runat="server">
      <h2>Current Node</h2>
      <asp:Label ID="Label_CurrentNode" Runat="Server"></asp:Label>
      <h2>Child Nodes</h2>
      <asp:Label ID="Label_ChildNodes" Runat="Server"></asp:Label>
      <h2>Verify Against Site Map</h2>
      <asp:SiteMapDataSource ID="SiteMapDataSource1" Runat="server" />
      <asp:TreeView ID="TreeView1" Runat="server" DataSourceID="SiteMapDataSource1">
      </asp:TreeView>
    </form>
  </body>
</html>
<%@ Page language="c#" AutoEventWireup="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
  private void Page_Load(object sender, System.EventArgs e)
  {
    try
    {
      string LabelText = "";
      // Displays the title of the current node.
      Label_CurrentNode.Text = SiteMap.CurrentNode.Title;
      // Determines if the current node has child nodes.
      if (SiteMap.CurrentNode.HasChildNodes)
      {
        foreach (SiteMapNode childNodesEnumerator in SiteMap.CurrentNode.ChildNodes)
        {
          // Displays the title of each node.
          LabelText = LabelText + childNodesEnumerator.Title + "<br />";
        }
      }
      Label_ChildNodes.Text = LabelText;
    }
    catch (System.NullReferenceException ex)
    {
      Label_CurrentNode.Text = "The current file is not in the site map.";
    }
    catch (Exception ex)
    {
      Label_CurrentNode.Text = "Generic exception: " + e.ToString();
    }
  }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head>
    <title>Enumerating Child Site Map Nodes</title>
  </head>
  <body>
    <form id="Form1" method="post" runat="server">
      <h2>Current Node</h2>
      <asp:Label id="Label_CurrentNode" runat="Server"></asp:Label>
      <h2>Child Nodes</h2>
      <asp:Label id="Label_ChildNodes" runat="Server"></asp:Label>
      <h2>Verify Against Site Map</h2>
      <asp:SiteMapDataSource id="SiteMapDataSource1" runat="server" />
      <asp:TreeView id="TreeView1" runat="server" dataSourceID="SiteMapDataSource1">
      </asp:TreeView>
    </form>
  </body>
</html>
安全性
可以向特定安全角色的用户隐藏导航结构中的链接。有关更多信息,请参见 ASP.NET 站点地图安全性调整。