SiteMapProvider 类

定义

为所有站点地图数据提供程序提供公共基类,并为开发人员提供一条实现自定义站点地图数据提供程序的途径,这些提供程序可与 ASP.NET 站点地图基础结构一起用作 SiteMap 对象的持久存储区。

public ref class SiteMapProvider abstract : System::Configuration::Provider::ProviderBase
public abstract class SiteMapProvider : System.Configuration.Provider.ProviderBase
type SiteMapProvider = class
    inherit ProviderBase
Public MustInherit Class SiteMapProvider
Inherits ProviderBase
继承
SiteMapProvider
派生

示例

下面的代码示例演示如何编写实现抽象 SiteMapProvider 类的类。 此示例仅包含一个示例 SiteMapProvider 和一个与之配合使用的示例文本文件。 若要运行该示例,还需要 Web.config 文件中的条目和.aspx页。 可以在 属性的文档 SiteMapDataSource.SiteMapProvider 中找到这些属性。

该示例使用一个逗号分隔的文件,该文件遵循预期结构来加载站点地图信息。 文件的第一行表示站点地图的根节点,后续行是子节点。 每个子节点按 URL 标识其父节点。 满足这些条件的文件示例如下所示。

default.aspx,Home,MyCompany Home Page,  
sale.aspx,Now On Sale,Check Out These Great Deals!,default.aspx  
catalog.aspx,Online Catalog,Browse Our Many Great Items!,default.aspx  

SimpleTextSiteMapProvider提供了所有SiteMapProvider属性和方法的示例实现。

using System;
using System.Configuration.Provider;
using System.Collections;
using System.Collections.Specialized;
using System.IO;
using System.Security.Permissions;
using System.Web;

namespace Samples.AspNet.CS
{

  [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
  public class SimpleTextSiteMapProvider : SiteMapProvider
  {
    private SiteMapProvider parentSiteMapProvider = null;
    private string simpleTextProviderName = null;
    private string sourceFilename = null;
    private SiteMapNode rootNode = null;
    private ArrayList siteMapNodes = null;
    private ArrayList childParentRelationship = null;

    // A default constructor. The Name property is initialized in the
    // Initialize method.
    public SimpleTextSiteMapProvider()
    {
    }
    // Implement the CurrentNode property.
    public override SiteMapNode CurrentNode
    {
      get
      {
        string currentUrl = FindCurrentUrl();
        // Find the SiteMapNode that represents the current page.
        SiteMapNode currentNode = FindSiteMapNode(currentUrl);
        return currentNode;
      }
    }

    // Implement the RootNode property.
    public override SiteMapNode RootNode
    {
      get
      {
        return rootNode;
      }
    }
    // Implement the ParentProvider property.
    public override SiteMapProvider ParentProvider
    {
      get
      {
        return parentSiteMapProvider;
      }
      set
      {
        parentSiteMapProvider = value;
      }
    }

    // Implement the RootProvider property.
    public override SiteMapProvider RootProvider
    {
      get
      {
        // If the current instance belongs to a provider hierarchy, it
        // cannot be the RootProvider. Rely on the ParentProvider.
        if (this.ParentProvider != null)
        {
          return ParentProvider.RootProvider;
        }
        // If the current instance does not have a ParentProvider, it is
        // not a child in a hierarchy, and can be the RootProvider.
        else
        {
          return this;
        }
      }
    }
    // Implement the FindSiteMapNode method.
    public override SiteMapNode FindSiteMapNode(string rawUrl)
    {

      // Does the root node match the URL?
      if (RootNode.Url == rawUrl)
      {
        return RootNode;
      }
      else
      {
        SiteMapNode candidate = null;
        // Retrieve the SiteMapNode that matches the URL.
        lock (this)
        {
          candidate = GetNode(siteMapNodes, rawUrl);
        }
        return candidate;
      }
    }
    // Implement the GetChildNodes method.
    public override SiteMapNodeCollection GetChildNodes(SiteMapNode node)
    {
      SiteMapNodeCollection children = new SiteMapNodeCollection();
      // Iterate through the ArrayList and find all nodes that have the specified node as a parent.
      lock (this)
      {
        for (int i = 0; i < childParentRelationship.Count; i++)
        {

          string nodeUrl = ((DictionaryEntry)childParentRelationship[i]).Key as string;

          SiteMapNode parent = GetNode(childParentRelationship, nodeUrl);

          if (parent != null && node.Url == parent.Url)
          {
            // The SiteMapNode with the Url that corresponds to nodeUrl
            // is a child of the specified node. Get the SiteMapNode for
            // the nodeUrl.
            SiteMapNode child = FindSiteMapNode(nodeUrl);
            if (child != null)
            {
              children.Add(child as SiteMapNode);
            }
            else
            {
              throw new Exception("ArrayLists not in sync.");
            }
          }
        }
      }
      return children;
    }
    protected override SiteMapNode GetRootNodeCore()
    {
      return RootNode;
    }
    // Implement the GetParentNode method.
    public override SiteMapNode GetParentNode(SiteMapNode node)
    {
      // Check the childParentRelationship table and find the parent of the current node.
      // If there is no parent, the current node is the RootNode.
      SiteMapNode parent = null;
      lock (this)
      {
        // Get the Value of the node in childParentRelationship
        parent = GetNode(childParentRelationship, node.Url);
      }
      return parent;
    }

    // Implement the ProviderBase.Initialize property.
    // Initialize is used to initialize the state that the Provider holds, but
    // not actually build the site map.
    public override void Initialize(string name, NameValueCollection attributes)
    {

      lock (this)
      {

        base.Initialize(name, attributes);

        simpleTextProviderName = name;
        sourceFilename = attributes["siteMapFile"];
        siteMapNodes = new ArrayList();
        childParentRelationship = new ArrayList();

        // Build the site map in memory.
        LoadSiteMapFromStore();
      }
    }
    // Private helper methods

    private SiteMapNode GetNode(ArrayList list, string url)
    {
      for (int i = 0; i < list.Count; i++)
      {
        DictionaryEntry item = (DictionaryEntry)list[i];
        if ((string)item.Key == url)
          return item.Value as SiteMapNode;
      }
      return null;
    }

    // Get the URL of the currently displayed page.
    private string FindCurrentUrl()
    {
      try
      {
        // The current HttpContext.
        HttpContext currentContext = HttpContext.Current;
        if (currentContext != null)
        {
          return currentContext.Request.RawUrl;
        }
        else
        {
          throw new Exception("HttpContext.Current is Invalid");
        }
      }
      catch (Exception e)
      {
        throw new NotSupportedException("This provider requires a valid context.",e);
      }
    }
    protected virtual void LoadSiteMapFromStore()
    {
      string pathToOpen;

      lock (this)
      {
        // If a root node exists, LoadSiteMapFromStore has already
        // been called, and the method can return.
        if (rootNode != null)
        {
          return;
        }
        else
        {
          pathToOpen = HttpContext.Current.Server.MapPath("~" + "\\" + sourceFilename);

          if (File.Exists(pathToOpen))
          {
            // Open the file to read from.
            using (StreamReader sr = File.OpenText(pathToOpen))
            {

              // Clear the state of the collections and rootNode
              rootNode = null;
              siteMapNodes.Clear();
              childParentRelationship.Clear();

              // Parse the file and build the site map
              string s = "";
              string[] nodeValues = null;
              SiteMapNode temp = null;

              while ((s = sr.ReadLine()) != null)
              {

                // Build the various SiteMapNode objects and add
                // them to the ArrayList collections. The format used
                // is: URL,TITLE,DESCRIPTION,PARENTURL

                nodeValues = s.Split(',');

                temp = new SiteMapNode(this,
                    HttpRuntime.AppDomainAppVirtualPath + "/" + nodeValues[0],
                    HttpRuntime.AppDomainAppVirtualPath + "/" + nodeValues[0],
                    nodeValues[1],
                    nodeValues[2]);

                // Is this a root node yet?
                if (null == rootNode &&
                    string.IsNullOrEmpty(nodeValues[3]))
                {
                  rootNode = temp;
                }

              // If not the root node, add the node to the various collections.
                else
                {
                  siteMapNodes.Add(new DictionaryEntry(temp.Url, temp));
                  // The parent node has already been added to the collection.
                  SiteMapNode parentNode =
                           FindSiteMapNode(HttpRuntime.AppDomainAppVirtualPath + "/" + nodeValues[3]);
                  if (parentNode != null)
                  {
                    childParentRelationship.Add(new DictionaryEntry(temp.Url, parentNode));
                  }
                  else
                  {
                    throw new Exception("Parent node not found for current node.");
                  }
                }
              }
            }
          }
          else
          {
            throw new Exception("File not found");
          }
        }
      }
      return;
    }
  }
}
Imports System.Collections
Imports System.Collections.Specialized
Imports System.Configuration.Provider
Imports System.IO
Imports System.Security.Permissions
Imports System.Web

Namespace Samples.AspNet.VB

  <AspNetHostingPermission(SecurityAction.Demand, Level:=AspNetHostingPermissionLevel.Minimal)> _
  Public Class SimpleTextSiteMapProvider
    Inherits SiteMapProvider

    Private parentSiteMapProvider As SiteMapProvider = Nothing
    Private simpleTextProviderName As String = Nothing
    Private sourceFilename As String = Nothing
    Private aRootNode As SiteMapNode = Nothing
    Private siteMapNodes As ArrayList = Nothing
    Private childParentRelationship As ArrayList = Nothing

    ' A default constructor. The Name property is initialized in the
    ' Initialize method.
    Public Sub New()
    End Sub

    ' Implement the CurrentNode property.
    Public Overrides ReadOnly Property CurrentNode() As SiteMapNode
      Get
        Dim currentUrl As String = FindCurrentUrl()
        ' Find the SiteMapNode that represents the current page.
        Dim aCurrentNode As SiteMapNode = FindSiteMapNode(currentUrl)
        Return aCurrentNode
      End Get
    End Property

    ' Implement the RootNode property.
    Public Overrides ReadOnly Property RootNode() As SiteMapNode
      Get
        Return aRootNode
      End Get
    End Property

    ' Implement the ParentProvider property.
    Public Overrides Property ParentProvider() As SiteMapProvider
      Get
        Return parentSiteMapProvider
      End Get
      Set(ByVal value As SiteMapProvider)
        parentSiteMapProvider = Value
      End Set
    End Property

    ' Implement the RootProvider property.
    Public Overrides ReadOnly Property RootProvider() As SiteMapProvider
      Get
        ' If the current instance belongs to a provider hierarchy, it
        ' cannot be the RootProvider. Rely on the ParentProvider.
        If Not (Me.ParentProvider Is Nothing) Then
          Return ParentProvider.RootProvider
          ' If the current instance does not have a ParentProvider, it is
          ' not a child in a hierarchy, and can be the RootProvider.
        Else
          Return Me
        End If
      End Get
    End Property

    ' Implement the FindSiteMapNode method.
    Public Overrides Function FindSiteMapNode(ByVal rawUrl As String) As SiteMapNode
      ' Does the root node match the URL?
      If RootNode.Url = rawUrl Then
        Return RootNode
      Else
        Dim candidate As SiteMapNode = Nothing
        ' Retrieve the SiteMapNode that matches the URL.
        SyncLock Me
          candidate = GetNode(siteMapNodes, rawUrl)
        End SyncLock
        Return candidate
      End If
    End Function 'FindSiteMapNode

    ' Implement the GetChildNodes method.
    Public Overrides Function GetChildNodes(ByVal node As SiteMapNode) As SiteMapNodeCollection
      Dim children As New SiteMapNodeCollection()
      ' Iterate through the ArrayList and find all nodes that have the specified node as a parent.
      SyncLock Me
        Dim i As Integer
        For i = 0 To childParentRelationship.Count - 1

          Dim de As DictionaryEntry = CType(childParentRelationship(i), DictionaryEntry)
          Dim nodeUrl As String = CType(de.Key, String)

          Dim parent As SiteMapNode = GetNode(childParentRelationship, nodeUrl)

          If Not (parent Is Nothing) AndAlso node.Url = parent.Url Then
            ' The SiteMapNode with the Url that corresponds to nodeUrl
            ' is a child of the specified node. Get the SiteMapNode for
            ' the nodeUrl.
            Dim child As SiteMapNode = FindSiteMapNode(nodeUrl)
            If Not (child Is Nothing) Then
              children.Add(CType(child, SiteMapNode))
            Else
              Throw New Exception("ArrayLists not in sync.")
            End If
          End If
        Next i
      End SyncLock
      Return children
    End Function 'GetChildNodes

    Protected Overrides Function GetRootNodeCore() As SiteMapNode
      Return RootNode
    End Function ' GetRootNodeCore()

    ' Implement the GetParentNode method.
    Public Overrides Function GetParentNode(ByVal node As SiteMapNode) As SiteMapNode
      ' Check the childParentRelationship table and find the parent of the current node.
      ' If there is no parent, the current node is the RootNode.
      Dim parent As SiteMapNode = Nothing
      SyncLock Me
        ' Get the Value of the node in childParentRelationship
        parent = GetNode(childParentRelationship, node.Url)
      End SyncLock
      Return parent
    End Function 'GetParentNode

    ' Implement the ProviderBase.Initialize method.
    ' Initialize is used to initialize the state that the Provider holds, but
    ' not actually build the site map.
    Public Overrides Sub Initialize(ByVal name As String, ByVal attributes As NameValueCollection)
      SyncLock Me
        MyBase.Initialize(name, attributes)
        simpleTextProviderName = name
        sourceFilename = attributes("siteMapFile")
        siteMapNodes = New ArrayList()
        childParentRelationship = New ArrayList()
        ' Build the site map in memory.
        LoadSiteMapFromStore()
      End SyncLock
    End Sub

    ' Private helper methods
    Private Function GetNode(ByVal list As ArrayList, ByVal url As String) As SiteMapNode
      Dim i As Integer
      For i = 0 To list.Count - 1
        Dim item As DictionaryEntry = CType(list(i), DictionaryEntry)
        If CStr(item.Key) = url Then
          Return CType(item.Value, SiteMapNode)
        End If
      Next i
      Return Nothing
    End Function 'GetNode


    ' Get the URL of the currently displayed page.
    Private Function FindCurrentUrl() As String
      Try
        ' The current HttpContext.
        Dim currentContext As HttpContext = HttpContext.Current
        If Not (currentContext Is Nothing) Then
          Return currentContext.Request.RawUrl
        Else
          Throw New Exception("HttpContext.Current is Invalid")
        End If
      Catch e As Exception
        Throw New NotSupportedException("This provider requires a valid context.", e)
      End Try
    End Function 'FindCurrentUrl

    Protected Overridable Sub LoadSiteMapFromStore()
      Dim pathToOpen As String
      SyncLock Me
        ' If a root node exists, LoadSiteMapFromStore has already
        ' been called, and the method can return.
        If Not (aRootNode Is Nothing) Then
          Return
        Else
          pathToOpen = HttpContext.Current.Server.MapPath("~" & "\\" & sourceFilename)
          If File.Exists(pathToOpen) Then
            ' Open the file to read from.
            Dim sr As StreamReader = File.OpenText(pathToOpen)
            Try

              ' Clear the state of the collections and aRootNode
              aRootNode = Nothing
              siteMapNodes.Clear()
              childParentRelationship.Clear()

              ' Parse the file and build the site map
              Dim s As String = ""
              Dim nodeValues As String() = Nothing
              Dim temp As SiteMapNode = Nothing

              Do
                s = sr.ReadLine()

                If Not s Is Nothing Then
                  ' Build the various SiteMapNode objects and add
                  ' them to the ArrayList collections. The format used
                  ' is: URL,TITLE,DESCRIPTION,PARENTURL
                  nodeValues = s.Split(","c)

                  temp = New SiteMapNode(Me, _
                      HttpRuntime.AppDomainAppVirtualPath & "/" & nodeValues(0), _
                      HttpRuntime.AppDomainAppVirtualPath & "/" & nodeValues(0), _
                      nodeValues(1), _
                      nodeValues(2))

                  ' Is this a root node yet?
                  If aRootNode Is Nothing AndAlso _
                    (nodeValues(3) Is Nothing OrElse _
                     nodeValues(3) = String.Empty) Then
                    aRootNode = temp

                    ' If not the root node, add the node to the various collections.
                  Else

                    siteMapNodes.Add(New DictionaryEntry(temp.Url, temp))

                    ' The parent node has already been added to the collection.
                    Dim parentNode As SiteMapNode = _
                        FindSiteMapNode(HttpRuntime.AppDomainAppVirtualPath & "/" & nodeValues(3))

                    If Not (parentNode Is Nothing) Then
                      childParentRelationship.Add(New DictionaryEntry(temp.Url, parentNode))
                    Else
                      Throw New Exception("Parent node not found for current node.")
                    End If
                  End If
                End If
              Loop Until s Is Nothing
            Finally
              sr.Close()
            End Try
          Else
            Throw New Exception("File not found")
          End If
        End If
      End SyncLock
      Return
    End Sub
  End Class
End Namespace

注解

StaticSiteMapProviderXmlSiteMapProvider 类表示抽象SiteMapProvider类的默认实现。 XmlSiteMapProvider使用名为 Web.sitemap 的 XML 文件来存储站点地图数据。 有关 Web.sitemap 文件的详细信息,请参阅 ASP.NET 站点地图

SiteMapProvider 通过声明 RootProviderParentProvider 属性支持站点地图提供程序层次结构的概念。 SiteMapProvider可以是另一个提供程序的子级或父级。 这样,网站的不同内容区域由维护其自己的站点地图和站点地图提供程序的不同开发组拥有或实现。

所有 SiteMapProvider 对象都在 Web.config 文件中配置。 在这些配置文件中声明的任何站点地图提供程序在运行时加载,并用于加载和处理站点导航数据。 对象 SiteMap 跟踪通过其 Providers 属性集合提供的所有提供程序,提供对提供程序管理的导航数据的编程访问。 下面的代码示例演示用于在 Web.config 文件中声明站点地图提供程序的格式。

<siteMap defaultProvider="<name>">  
  <providers>  
    <add  
      name="<friendly name>"  
      type="<fully qualified class name>, <assembly name (optional)>"   
      siteMapFile = "<file name>" />  
  </providers>  
</siteMap>  

这些提供程序加载的网站导航数据由站点地图基础结构的其他组件(如 SiteMapPathTreeView 控件)用于显示用户的站点地图信息。

如果实现自己的站点地图提供程序,则可以将源文件放置在 ASP.NET 应用程序的 App_Code 目录中,然后将自动编译程序集。 还可以将自己的站点地图提供程序放置在全局程序集缓存 (GAC) 中,并在 Web.config 文件中提供对其的完全限定引用。 有关编译器服务的详细信息,请参阅 使用程序集和全局程序集缓存

实施者说明

SiteMapProvider 类继承时,必须重写以下成员: GetRootNodeCore()FindSiteMapNode(String)GetChildNodes(SiteMapNode)GetParentNode(SiteMapNode)

构造函数

SiteMapProvider()

初始化 SiteMapProvider 类的新实例。

属性

CurrentNode

获取表示当前请求页的 SiteMapNode 对象。

Description

获取一条简短的易懂描述,它适合在管理工具或其他用户界面 (UI) 中显示。

(继承自 ProviderBase)
EnableLocalization

获取或设置一个布尔值,该值指示是否返回 SiteMapNode 特性的本地化的值。

Name

获得一个友好名称,用于在配置过程中引用提供程序。

(继承自 ProviderBase)
ParentProvider

获取或设置当前提供程序的父 SiteMapProvider 对象。

ResourceKey

获取或设置用于本地化 SiteMapNode 特性的资源键。

RootNode

获取当前提供程序表示的站点地图数据的根 SiteMapNode 对象。

RootProvider

获取当前提供程序层次结构中的根 SiteMapProvider 对象。

SecurityTrimmingEnabled

获取一个布尔值,该值指示站点地图提供程序是否根据用户的角色筛选站点地图节点。

方法

AddNode(SiteMapNode)

将一个 SiteMapNode 对象添加到由站点地图提供程序维护的节点集合。

AddNode(SiteMapNode, SiteMapNode)

将一个 SiteMapNode 对象添加到由站点地图提供程序维护的节点集合并指定父 SiteMapNode 对象。

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
FindSiteMapNode(HttpContext)

使用指定的 SiteMapNode 对象检索表示当前请求页的 HttpContext 对象。

FindSiteMapNode(String)

当在派生类中被重写时,将检索表示位于指定 URL 的页的 SiteMapNode 对象。

FindSiteMapNodeFromKey(String)

根据指定的键检索 SiteMapNode 对象。

GetChildNodes(SiteMapNode)

当在派生类中被重写时,将检索特定 SiteMapNode 的子节点。

GetCurrentNodeAndHintAncestorNodes(Int32)

在站点地图提供程序检索当前请求页的节点并获取当前页的父和祖先站点地图节点时,为其提供经优化的查找方法。

GetCurrentNodeAndHintNeighborhoodNodes(Int32, Int32)

在站点地图提供程序检索当前请求页的节点并获取当前节点附近的站点地图节点时,为其提供经优化的查找方法。

GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetParentNode(SiteMapNode)

当在派生类中被重写时,将检索特定 SiteMapNode 对象的父节点。

GetParentNodeRelativeToCurrentNodeAndHintDownFromParent(Int32, Int32)

在站点地图提供程序检索当前请求页的祖先节点并获取其子代节点时,为其提供经优化的查找方法。

GetParentNodeRelativeToNodeAndHintDownFromParent(SiteMapNode, Int32, Int32)

在站点地图提供程序检索指定 SiteMapNode 对象的祖先节点并获取其子节点时,为其提供经优化的查找方法。

GetRootNodeCore()

当在派生类中被重写时,将检索目前由当前提供者管理的所有节点的根节点。

GetRootNodeCoreFromProvider(SiteMapProvider)

检索当前由指定的站点地图提供程序管理的所有节点的根节点。

GetType()

获取当前实例的 Type

(继承自 Object)
HintAncestorNodes(SiteMapNode, Int32)

提供一个方法,站点地图提供程序重写该方法来执行相对于指定 SiteMapNode 对象的一个或多个层次的父和祖先节点的经优化的检索。

HintNeighborhoodNodes(SiteMapNode, Int32, Int32)

提供一个方法,站点地图提供程序可以重写该方法以执行在指定节点附近找到的节点的经优化的检索。

Initialize(String, NameValueCollection)

初始化 SiteMapProvider 实现(包括从持久性存储区加载站点地图数据所需的任何资源)。

IsAccessibleToUser(HttpContext, SiteMapNode)

检索一个布尔值,该值指示指定的 SiteMapNode 对象是否可由用户在指定的上下文中查看。

MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
RemoveNode(SiteMapNode)

从由站点地图提供程序维护的节点集合移除指定的 SiteMapNode 对象。

ResolveSiteMapNode(HttpContext)

引发 SiteMapResolve 事件。

ToString()

返回表示当前对象的字符串。

(继承自 Object)

事件

SiteMapResolve

当调用 CurrentNode 属性时发生。

适用于

另请参阅