StaticSiteMapProvider 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
做為抽象 SiteMapProvider 類別的部分實作,以及做為 ASP.NET 預設網站導覽提供者 XmlSiteMapProvider 類別的基底類別。
public ref class StaticSiteMapProvider abstract : System::Web::SiteMapProvider
public abstract class StaticSiteMapProvider : System.Web.SiteMapProvider
type StaticSiteMapProvider = class
inherit SiteMapProvider
Public MustInherit Class StaticSiteMapProvider
Inherits SiteMapProvider
- 繼承
- 衍生
範例
下列程式代碼範例示範如何擴充 StaticSiteMapProvider 類別,以使用 Microsoft Access 作為網站地圖提供者。 類別 AccessSiteMapProvider
是僅支持簡單、一層深層階層的網站地圖提供者。 網站地圖數據儲存所在的數據表具有下列結構:
NODEID URL NAME PARENTNODEID
---------------------------------------------
1 default.aspx Default <NULL>
2 catalog.aspx Catalog 1
3 aboutus.aspx Contact Us 1
...
類別 AccessSiteMapProvider
衍生自 類別, StaticSiteMapProvider 並使用基本 SQL 查詢和 OleDbCommand 和 OleDbDataReader 物件,從 Microsoft Access 資料庫擷取其資訊。
#using <System.Data.dll>
#using <System.Transactions.dll>
#using <System.EnterpriseServices.dll>
#using <System.dll>
#using <System.Web.dll>
#using <System.Configuration.dll>
using namespace System;
using namespace System::Collections;
using namespace System::Collections::Specialized;
using namespace System::Configuration;
using namespace System::Data;
using namespace System::Data::OleDb;
using namespace System::Security::Permissions;
using namespace System::Web;
/// An extremely simple AccessSiteMapProvider that only supports a
/// site map node hierarchy 1 level deep.
[AspNetHostingPermission(SecurityAction::Demand,Level=AspNetHostingPermissionLevel::Minimal)]
public ref class AccessSiteMapProvider: public StaticSiteMapProvider
{
private:
SiteMapNode ^ rootNode;
OleDbConnection^ accessConnection;
// This string is case sensitive.
String^ AccessConnectionStringName;
public:
// Implement a default constructor.
AccessSiteMapProvider()
{
initialized = false;
AccessConnectionStringName = "accessSiteMapConnectionString";
}
private:
// Some basic state to help track the initialization state of the provider.
bool initialized;
public:
property bool IsInitialized
{
virtual bool get()
{
return initialized;
}
}
property SiteMapNode ^ RootNode
{
// Return the root node of the current site map.
virtual SiteMapNode ^ get() override
{
SiteMapNode ^ temp = nullptr;
temp = BuildSiteMap();
return temp;
}
}
protected:
virtual SiteMapNode ^ GetRootNodeCore() override
{
return RootNode;
}
public:
// Initialize is used to initialize the properties and any state that the
// AccessProvider holds, but is not used to build the site map.
// The site map is built when the BuildSiteMap method is called.
virtual void Initialize( String^ name, NameValueCollection^ attributes ) override
{
if ( IsInitialized )
return;
StaticSiteMapProvider::Initialize( name, attributes );
// Create and test the connection to the Microsoft Access database.
// Retrieve the Value of the Access connection string from the
// attributes NameValueCollection.
String^ connectionString = attributes[ AccessConnectionStringName ];
if ( nullptr == connectionString || connectionString->Length == 0 )
throw gcnew Exception( "The connection string was not found." );
else
accessConnection = gcnew OleDbConnection( connectionString );
initialized = true;
}
protected:
///
/// SiteMapProvider and StaticSiteMapProvider methods that this derived class must override.
///
// Clean up any collections or other state that an instance of this may hold.
virtual void Clear() override
{
System::Threading::Monitor::Enter( this );
try
{
rootNode = nullptr;
StaticSiteMapProvider::Clear();
}
finally
{
System::Threading::Monitor::Exit( this );
}
}
public:
// Build an in-memory representation from persistent
// storage, and return the root node of the site map.
virtual SiteMapNode ^ BuildSiteMap() override
{
// Since the SiteMap class is static, make sure that it is
// not modified while the site map is built.
System::Threading::Monitor::Enter( this );
try
{
// If there is no initialization, this method is being
// called out of order.
if ( !IsInitialized )
{
throw gcnew Exception( "BuildSiteMap called incorrectly." );
}
// If there is no root node, then there is no site map.
if ( nullptr == rootNode )
{
// Start with a clean slate
Clear();
// Select the root node of the site map from Microsoft Access.
int rootNodeId = -1;
if ( accessConnection->State == ConnectionState::Closed )
accessConnection->Open();
OleDbCommand^ rootNodeCommand = gcnew OleDbCommand
("SELECT nodeid, url, name FROM SiteMap WHERE parentnodeid IS NULL", accessConnection);
OleDbDataReader^ rootNodeReader = rootNodeCommand->ExecuteReader();
if ( rootNodeReader->HasRows )
{
rootNodeReader->Read();
rootNodeId = rootNodeReader->GetInt32( 0 );
// Create a SiteMapNode that references the current StaticSiteMapProvider.
rootNode = gcnew SiteMapNode(this, rootNodeId.ToString(),
rootNodeReader->GetString( 1 ),rootNodeReader->GetString( 2 ));
}
else
return nullptr;
rootNodeReader->Close();
// Select the child nodes of the root node.
OleDbCommand^ childNodesCommand = gcnew OleDbCommand
("SELECT nodeid, url, name FROM SiteMap WHERE parentnodeid = ?", accessConnection);
OleDbParameter^ rootParam = gcnew OleDbParameter( "parentid", OleDbType::Integer);
rootParam->Value = rootNodeId;
childNodesCommand->Parameters->Add( rootParam );
OleDbDataReader^ childNodesReader = childNodesCommand->ExecuteReader();
if ( childNodesReader->HasRows )
{
SiteMapNode ^ childNode = nullptr;
while ( childNodesReader->Read() )
{
childNode = gcnew SiteMapNode( this,
System::Convert::ToString(childNodesReader->GetInt32( 0 )),
childNodesReader->GetString( 1 ),
childNodesReader->GetString( 2 )
);
// Use the SiteMapNode AddNode method to add
// the SiteMapNode to the ChildNodes collection.
AddNode( childNode, rootNode );
}
}
childNodesReader->Close();
accessConnection->Close();
}
return rootNode;
}
finally
{
System::Threading::Monitor::Exit( this );
}
}
};
namespace Samples.AspNet.CS.Controls {
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Data;
using System.Data.OleDb;
using System.Security.Permissions;
using System.Web;
/// An extremely simple AccessSiteMapProvider that only supports a
/// site map node hierarchy 1 level deep.
[AspNetHostingPermission(SecurityAction.Demand, Level=AspNetHostingPermissionLevel.Minimal)]
public class AccessSiteMapProvider : StaticSiteMapProvider
{
private SiteMapNode rootNode = null;
private OleDbConnection accessConnection = null;
// This string is case sensitive.
private string AccessConnectionStringName = "accessSiteMapConnectionString";
// Implement a default constructor.
public AccessSiteMapProvider () { }
// Some basic state to help track the initialization state of the provider.
private bool initialized = false;
public virtual bool IsInitialized {
get {
return initialized;
}
}
// Return the root node of the current site map.
public override SiteMapNode RootNode {
get {
SiteMapNode temp = null;
temp = BuildSiteMap();
return temp;
}
}
protected override SiteMapNode GetRootNodeCore() {
return RootNode;
}
// Initialize is used to initialize the properties and any state that the
// AccessProvider holds, but is not used to build the site map.
// The site map is built when the BuildSiteMap method is called.
public override void Initialize(string name, NameValueCollection attributes) {
if (IsInitialized)
return;
base.Initialize(name, attributes);
// Create and test the connection to the Microsoft Access database.
// Retrieve the Value of the Access connection string from the
// attributes NameValueCollection.
string connectionString = attributes[AccessConnectionStringName];
if (null == connectionString || connectionString.Length == 0)
throw new Exception ("The connection string was not found.");
else
accessConnection = new OleDbConnection(connectionString);
initialized = true;
}
///
/// SiteMapProvider and StaticSiteMapProvider methods that this derived class must override.
///
// Clean up any collections or other state that an instance of this may hold.
protected override void Clear() {
lock (this) {
rootNode = null;
base.Clear();
}
}
// Build an in-memory representation from persistent
// storage, and return the root node of the site map.
public override SiteMapNode BuildSiteMap() {
// Since the SiteMap class is static, make sure that it is
// not modified while the site map is built.
lock(this) {
// If there is no initialization, this method is being
// called out of order.
if (! IsInitialized) {
throw new Exception("BuildSiteMap called incorrectly.");
}
// If there is no root node, then there is no site map.
if (null == rootNode) {
// Start with a clean slate
Clear();
// Select the root node of the site map from Microsoft Access.
int rootNodeId = -1;
if (accessConnection.State == ConnectionState.Closed)
accessConnection.Open();
OleDbCommand rootNodeCommand =
new OleDbCommand("SELECT nodeid, url, name FROM SiteMap WHERE parentnodeid IS NULL",
accessConnection);
OleDbDataReader rootNodeReader = rootNodeCommand.ExecuteReader();
if(rootNodeReader.HasRows) {
rootNodeReader.Read();
rootNodeId = rootNodeReader.GetInt32(0);
// Create a SiteMapNode that references the current StaticSiteMapProvider.
rootNode = new SiteMapNode(this,
rootNodeId.ToString(),
rootNodeReader.GetString(1),
rootNodeReader.GetString(2));
}
else
{
return null;
}
rootNodeReader.Close();
// Select the child nodes of the root node.
OleDbCommand childNodesCommand =
new OleDbCommand("SELECT nodeid, url, name FROM SiteMap WHERE parentnodeid = ?",
accessConnection);
OleDbParameter rootParam = new OleDbParameter("parentid", OleDbType.Integer);
rootParam.Value = rootNodeId;
childNodesCommand.Parameters.Add(rootParam);
OleDbDataReader childNodesReader = childNodesCommand.ExecuteReader();
if (childNodesReader.HasRows) {
SiteMapNode childNode = null;
while(childNodesReader.Read()) {
childNode = new SiteMapNode(this,
childNodesReader.GetInt32(0).ToString(),
childNodesReader.GetString(1),
childNodesReader.GetString(2));
// Use the SiteMapNode AddNode method to add
// the SiteMapNode to the ChildNodes collection.
AddNode(childNode, rootNode);
}
}
childNodesReader.Close();
accessConnection.Close();
}
return rootNode;
}
}
}
}
Imports System.Collections
Imports System.Collections.Specialized
Imports System.Data
Imports System.Data.OleDb
Imports System.Security.Permissions
Imports System.Web
Namespace Samples.AspNet.VB.Controls
' An extremely simple AccessSiteMapProvider that only supports a
' site map node hierarchy one level deep.
<AspNetHostingPermission(SecurityAction.Demand, Level:=AspNetHostingPermissionLevel.Minimal)> _
Public Class AccessSiteMapProvider
Inherits StaticSiteMapProvider
Private aRootNode As SiteMapNode = Nothing
Private accessConnection As OleDbConnection = Nothing
' This string is case sensitive.
Private AccessConnectionStringName As String = "accessSiteMapConnectionString"
' Implement a default constructor.
Public Sub New()
End Sub
' Some basic state to help track the initialization state of the provider.
Private initialized As Boolean = False
Public Overridable ReadOnly Property IsInitialized() As Boolean
Get
Return initialized
End Get
End Property
' Return the root node of the current site map.
Public Overrides ReadOnly Property RootNode() As SiteMapNode
Get
Return BuildSiteMap()
End Get
End Property
Protected Overrides Function GetRootNodeCore() As SiteMapNode
Return RootNode
End Function
' Initialize is used to initialize the properties and any state that the
' AccessProvider holds, but is not used to build the site map.
' The site map is built when the BuildSiteMap method is called.
Public Overrides Sub Initialize(ByVal name As String, ByVal attributes As NameValueCollection)
If IsInitialized Then
Return
End If
MyBase.Initialize(name, attributes)
' Create and test the connection to the Microsoft Access database.
' Retrieve the Value of the Access connection string from the
' attributes NameValueCollection.
Dim connectionString As String = attributes(AccessConnectionStringName)
If Nothing = connectionString OrElse connectionString.Length = 0 Then
Throw New Exception("The connection string was not found.")
Else
accessConnection = New OleDbConnection(connectionString)
End If
initialized = True
End Sub
' SiteMapProvider and StaticSiteMapProvider methods that this derived class must override.
'
' Clean up any collections or other state that an instance of this may hold.
Protected Overrides Sub Clear()
SyncLock Me
aRootNode = Nothing
MyBase.Clear()
End SyncLock
End Sub
' Build an in-memory representation from persistent
' storage, and return the root node of the site map.
Public Overrides Function BuildSiteMap() As SiteMapNode
' Since the SiteMap class is static, make sure that it is
' not modified while the site map is built.
SyncLock Me
' If there is no initialization, this method is being
' called out of order.
If Not IsInitialized Then
Throw New Exception("BuildSiteMap called incorrectly.")
End If
' If there is no root node, then there is no site map.
If aRootNode Is Nothing Then
' Start with a clean slate
Clear()
' Select the root node of the site map from Microsoft Access.
Dim rootNodeId As Integer = -1
If accessConnection.State = ConnectionState.Closed Then
accessConnection.Open()
End If
Dim rootNodeCommand As New OleDbCommand("SELECT nodeid, url, name FROM SiteMap WHERE parentnodeid IS NULL", accessConnection)
Dim rootNodeReader As OleDbDataReader = rootNodeCommand.ExecuteReader()
If rootNodeReader.HasRows Then
rootNodeReader.Read()
rootNodeId = rootNodeReader.GetInt32(0)
' Create a SiteMapNode that references the current StaticSiteMapProvider.
aRootNode = New SiteMapNode(Me, rootNodeId.ToString(), rootNodeReader.GetString(1), rootNodeReader.GetString(2))
Else
Return Nothing
End If
rootNodeReader.Close()
' Select the child nodes of the root node.
Dim childNodesCommand As New OleDbCommand("SELECT nodeid, url, name FROM SiteMap WHERE parentnodeid = ?", accessConnection)
Dim rootParam As New OleDbParameter("parentid", OleDbType.Integer)
rootParam.Value = rootNodeId
childNodesCommand.Parameters.Add(rootParam)
Dim childNodesReader As OleDbDataReader = childNodesCommand.ExecuteReader()
If childNodesReader.HasRows Then
Dim childNode As SiteMapNode = Nothing
While childNodesReader.Read()
childNode = New SiteMapNode(Me, _
childNodesReader.GetInt32(0).ToString(), _
childNodesReader.GetString(1), _
childNodesReader.GetString(2))
' Use the SiteMapNode AddNode method to add
' the SiteMapNode to the ChildNodes collection.
AddNode(childNode, aRootNode)
End While
End If
childNodesReader.Close()
accessConnection.Close()
End If
Return aRootNode
End SyncLock
End Function 'BuildSiteMap
End Class
End Namespace
最後,會 AccessSiteMapProvider
設定為下列 Web.config 檔案中的預設提供者。
<configuration>
<system.web>
<siteMap defaultProvider="AccessSiteMapProvider">
<providers>
<add
name="AccessSiteMapProvider"
type="Samples.AspNet.AccessSiteMapProvider,Samples.AspNet "
accessSiteMapConnectionString="PROVIDER=MICROSOFT.JET.OLEDB.4.0;DATA SOURCE=sitemap.mdb "/>
</providers>
</siteMap>
</system.web>
</configuration>
備註
類別 StaticSiteMapProvider 是抽象 SiteMapProvider 類的部分實作,並提供兩個額外的方法: AddNode 和 RemoveNode,以及抽象 BuildSiteMap 和受保護的 Clear 方法。
類別 StaticSiteMapProvider 支援撰寫網站地圖提供者 (例如, XmlSiteMapProvider) 會將儲存在永續性記憶體中的網站地圖轉譯為儲存在記憶體中的網站地圖。 類別 StaticSiteMapProvider 提供儲存和擷取物件的基本實作 SiteMapNode 。
SiteMapProvider和 StaticSiteMapProvider 類別支持網站地圖提供者階層的概念,其中網站地圖提供者可以與其他網站地圖提供者有階層式關聯性。 此模式是使用 RootProvider 和 ParentProvider 屬性來實作。
類別 StaticSiteMapProvider 會將其 SiteMapNode 物件儲存在哈希表中,並在內部使用 SiteMapNode.Url 以網站地圖節點表示的頁面屬性做為索引鍵。 (如果網站地圖節點未指定 URL,則會使用自動產生的唯一索引鍵來追蹤。) 因此,您無法有網站地圖節點,其中具有相同 URL 的網站地圖節點會多次使用。 例如,嘗試使用 類別載入下列程式代碼範例 XmlSiteMapProvider 中說明的網站地圖節點,這是預設 ASP.NET 網站地圖提供者,或是衍生自 StaticSiteMapProvider 類別的任何網站地圖提供者將無法運作,因為使用AboutUs.aspx頁面多次。
<sitemap>
<sitemapnode title="Home" description="Home" url="default.aspx" >
<sitemapnode title="Catalog" description="Our catalog" url="catalog.aspx"/>
<sitemapnode title="About Us" description="All about our company" url="aboutus.aspx"/>
<sitemapnode title="Driving Directions" description="Directions to our store" url="aboutus.aspx"/>
</sitemapnode>
</sitemap>
如果您要擴充 StaticSiteMapProvider 類別,則三個最重要的方法是 GetRootNodeCore、 Initialize和 BuildSiteMap 方法。 Clear和 FindSiteMapNode 方法具有足以用於大部分自定義網站地圖提供者實作的預設實作。
系統會 Initialize 呼叫 方法來初始化衍生的網站地圖提供者,包括載入網站地圖數據所需的任何資源,但會嘗試在記憶體中建置網站地圖節點。 如果您的衍生類別使用檔案來儲存網站地圖數據,則可以在這裡執行任何檔案初始化。 如果網站地圖節點使用某種其他類型的數據存放區,例如關係資料庫,則可能會在這裡執行連線初始化。 其他屬性,例如放在組態中網站地圖提供者元素上的檔名或連接字串,是由 ASP.NET 組態系統處理,並使用 參數傳遞至 Initialize 方法 attributes
。
BuildSiteMap方法必須由衍生自 StaticSiteMapProvider 類別的所有類別覆寫,並呼叫 以從永續性記憶體載入網站地圖節點,並將其轉換成內部表示法。 方法BuildSiteMap會在和 XmlSiteMapProvider 類別的許多預設成員實StaticSiteMapProvider作中於內部呼叫。 如果您實作自己的網站地圖提供者,請確定網站地圖數據處理發生一次,如果已經載入網站地圖資訊,後續呼叫 BuildSiteMap 方法就會立即傳回。 當您實作 BuildSiteMap 方法時,請確定它是安全線程,因為多個並行頁面要求可能會間接產生多個呼叫以載入網站地圖資訊。 網站地圖基礎結構支持根據使用者的角色顯示網站地圖資訊。 Roles視個別SiteMapNode物件所支持的屬性而定,不同用戶的導覽結構可能會存在。 類別之網站地圖節點擷取成員 StaticSiteMapProvider 的預設實作會藉由呼叫 IsAccessibleToUser 方法來自動執行安全性修剪。
和 AddNodeClearRemoveNode 方法會以安全線程的方式操作用來追蹤網站地圖節點的內部集合。
給實施者的注意事項
當您繼承自 類別時 StaticSiteMapProvider ,必須覆寫下列成員: BuildSiteMap()。
建構函式
StaticSiteMapProvider() |
初始化 StaticSiteMapProvider 類別的新執行個體。 |
屬性
CurrentNode |
取得代表目前所要求頁面的 SiteMapNode 物件。 (繼承來源 SiteMapProvider) |
Description |
取得簡短、易讀的描述,適合顯示在管理工具或其他使用者介面 (UI) 中。 (繼承來源 ProviderBase) |
EnableLocalization |
取得或設定布林值,指出是否傳回 SiteMapNode 屬性的當地語系化值。 (繼承來源 SiteMapProvider) |
Name |
取得用來在設定期間代表提供者的易記名稱。 (繼承來源 ProviderBase) |
ParentProvider |
取得或設定目前提供者的父代 SiteMapProvider 物件。 (繼承來源 SiteMapProvider) |
ResourceKey |
取得或設定用來當地語系化 SiteMapNode 屬性的資源索引鍵。 (繼承來源 SiteMapProvider) |
RootNode |
取得目前提供者所表示之網站導覽資料的根 SiteMapNode 物件。 (繼承來源 SiteMapProvider) |
RootProvider |
取得目前提供者階層架構中的根 SiteMapProvider 物件。 (繼承來源 SiteMapProvider) |
SecurityTrimmingEnabled |
取得布林值,指出網站導覽提供者是否根據使用者的角色篩選網站導覽節點。 (繼承來源 SiteMapProvider) |
方法
事件
SiteMapResolve |
呼叫 CurrentNode 屬性時發生。 (繼承來源 SiteMapProvider) |