SiteMap 類別

定義

SiteMap 類別是存放於記憶體之網站巡覽結構的表現方式,由一或多個網站導覽提供者所提供。The SiteMap class is an in-memory representation of the navigation structure for a site, which is provided by one or more site map providers. 此類別無法獲得繼承。This class cannot be inherited.

public ref class SiteMap abstract sealed
public static class SiteMap
type SiteMap = class
Public Class SiteMap
繼承
SiteMap

範例

本節包含兩個程式碼範例。This section contains two code examples. 第一個程式碼範例示範如何在 Web.config 檔案中宣告網站地圖提供者。The first code example demonstrates how to declare a site map provider in a Web.config file. 第二個程式碼範例示範如何使用 SiteMap 物件。The second code example demonstrates how to use a SiteMap object.

下列程式碼範例是 Web.config 設定區段,示範如何將 XmlSiteMapProvider 物件宣告為預設 SiteMapProvider 物件。The following code example is a Web.config configuration section that demonstrates how an XmlSiteMapProvider object is declared as the default SiteMapProvider object.

<siteMap defaultProvider="MyXmlSiteMapProvider" enabled="true">  
  <providers>  
    <add name="MyXmlSiteMapProvider"  
      description="The site map provider that reads in the .sitemap XML files."  
      type="System.Web.XmlSiteMapProvider, System.Web, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"  
      siteMapFile="my.sitemap" />  
  </providers>  
</siteMap>  

下列程式碼範例將示範如何藉 SiteMap 由存取其靜態成員來顯示和屬性的相關資訊,來使用物件 RootNode CurrentNodeThe following code example demonstrates how to use the SiteMap object by accessing its static members to display information about the RootNode and CurrentNode properties. 將它與控制項所呈現的輸出做比較 SiteMapPathCompare it to the output that is rendered by the SiteMapPath control.

<%@ Page language="c#"%>
<!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)
{
    // 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>
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="Form1" runat="server">
      <asp:SiteMapPath
          runat="server"
        ID="SiteMapPath1"
        ShowToolTips="false"/>
    </form>
  </body>
</html>
<%@ Page language="VB"%>
<!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(sender As Object, e As System.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>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="Form1" runat="server">
        <asp:SiteMapPath
            runat="server"
          ID="SiteMapPath1"
          ShowToolTips="false"/>

    </form>
  </body>
</html>

備註

網站地圖可搭配 SiteMapDataSourceSiteMapPath 和其他控制項使用,以呈現有效的流覽介面讓使用者流覽網站。Site maps are used in conjunction with the SiteMapDataSource, SiteMapPath, and other controls to render an effective navigation interface for users to navigate a Web site. SiteMap物件是 ASP.NET 網站導覽基礎結構的元件,可讓您使用流覽和控制項來存取頁面和控制項開發人員的唯讀網站對應資訊 SiteMapDataSourceA SiteMap object is a component of the ASP.NET site navigation infrastructure that provides access to read-only site map information for page and control developers using navigation and SiteMapDataSource controls. ASP.NET 網站導覽基礎結構的其他元件包括 SiteMapProviderXmlSiteMapProvider 類別、web.sitemap 檔案,以及使用該類別的各種控制項( SiteMap 例如 SiteMapPath 控制項),以呈現內容和功能表。Other components of the ASP.NET site navigation infrastructure include the SiteMapProvider and XmlSiteMapProvider classes, the Web.sitemap file, and the various controls that work with the SiteMap class, such as the SiteMapPath control, to render content and menus.

SiteMap有幾個函數:The SiteMap has several functions:

  • 它會提供網站導覽階層的根節點, (只能有一個根節點) 。It provides the root node of the site navigation hierarchy (there can be only one root node).

  • 它會識別哪個網站地圖提供者為主體或預設的提供者。It identifies which site map provider is the principal, or default, provider.

  • 它會追蹤所有用來建立的提供者物件 SiteMapIt keeps track of all the provider objects that are used to create the SiteMap.

基本上, SiteMap 是物件階層式集合的容器 SiteMapNodeFundamentally, the SiteMap is a container for a hierarchical collection of SiteMapNode objects. 但是,不 SiteMap 會維護節點之間的關聯性,而是會將此委派委派給網站地圖提供者。However, the SiteMap does not maintain the relationships between the nodes; rather, it delegates this to the site map providers. SiteMap作為網站導覽資訊的介面, (以 SiteMapNode 這些網站地圖提供者所持有的物件) 形式。The SiteMap acts as an interface to the site navigation information (in the form of SiteMapNode objects) that is held by these site map providers. 它會追蹤可從中存取網站導覽資料並公開其集合的提供者 ProvidersIt keeps track of the providers from which it can access site navigation data and exposes its Providers collection. 提供者可以用任何方式儲存其網站地圖資訊,只要它擴充 SiteMapProvider 抽象類別,就 SiteMap 可以成功初始化類別。A provider can store its site map information in any way - as long as it extends the SiteMapProvider abstract class, the SiteMap class can be initialized successfully from it.

類別的預設提供者 SiteMapXmlSiteMapProvider 可搭配 XML 設定檔(網站地圖)使用的類別。The default provider for the SiteMap class is the XmlSiteMapProvider class, which works with an XML configuration file, Web.sitemap. 不過,您可以撰寫自己的 SiteMapProvider 物件,並用它來初始化網站的網站地圖。However, you can write your own SiteMapProvider object, and use it to initialize the site map for your site. 如需自訂網站地圖提供者的範例,請參閱 SiteMapProviderFor an example of a custom site map provider, see SiteMapProvider.

指定預設的網站地圖設定,以管理網站導覽設定。A default site map configuration is specified that manages the site navigation configuration. 如果您選擇撰寫自己的網站地圖提供者,您可以在 Web.config 檔案中覆寫此設定。You can override this configuration in the Web.config file, should you choose to write your own site map provider.

SiteMap類別只會提供效能的靜態成員,並且可以在特定網站中,從 Web 表單或 web 伺服器控制項隨時存取。The SiteMap class offers only static members for performance and it can be accessed at any time in a given site from a Web Form or Web server control.

屬性

CurrentNode

取得 SiteMapNode 控制項,表示目前要求的網頁。Gets a SiteMapNode control that represents the currently requested page.

Enabled

取得布林值,指出 Web.config 檔案中是否已指定網站導覽提供者,以及網站導覽提供者是否已啟用。Gets a Boolean value indicating if a site map provider is specified in the Web.config file and if the site map provider is enabled.

Provider

取得目前網站導覽的預設 SiteMapProvider 物件。Gets the default SiteMapProvider object for the current site map.

Providers

取得名稱為 SiteMapProvider 物件的唯讀集合,這些物件可用於 SiteMap 類別。Gets a read-only collection of named SiteMapProvider objects that are available to the SiteMap class.

RootNode

取得 SiteMapNode 物件,表示網站巡覽結構最上層網頁。Gets a SiteMapNode object that represents the top-level page of the navigation structure for the site.

事件

SiteMapResolve

當存取 CurrentNode 屬性時發生。Occurs when the CurrentNode property is accessed.

適用於

另請參閱