SiteMapProvider.Initialize(String, NameValueCollection) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化 SiteMapProvider 實作,包括從持續性儲存體載入網站導覽資料時所需的任何資源。
public:
override void Initialize(System::String ^ name, System::Collections::Specialized::NameValueCollection ^ attributes);
public override void Initialize (string name, System.Collections.Specialized.NameValueCollection attributes);
override this.Initialize : string * System.Collections.Specialized.NameValueCollection -> unit
Public Overrides Sub Initialize (name As String, attributes As NameValueCollection)
參數
- attributes
- NameValueCollection
NameValueCollection,可以包含用來協助初始化提供者的其他屬性。 這些屬性是從 Web.config 檔案中的網站導覽提供者組態讀取。
範例
下列程式碼範例示範如何覆寫 Initialize 方法來準備 Microsoft Access 資料庫連線。
物件的連接字串 OleDbConnection 會在 方法的 Initialize 參數中 NameValueCollection 傳遞。 在此情況下,連接字串是由Web.config檔案中的提供者特定區段所提供。 在這裡, accessSiteMapConnectionString
包含裝載網站地圖資料的 Microsoft Access 資料庫的連接字串。
<siteMap defaultProvider="AccessSiteMapProvider">
<providers>
<add
name="AccessSiteMapProvider"
type="Samples.AspNet.AccessSiteMapProvider,Samples.AspNet"
accessSiteMapConnectionString="PROVIDER=MICROSOFT.JET.OLEDB.4.0;DATA SOURCE=\\SomeUNCShare\\sitemap.mdb"/>
</providers>
</siteMap>
此程式碼範例是提供給 類別之較大範例的 SiteMapProvider 一部分。
// 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:
// 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;
}
' 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
備註
方法 Initialize 實際上不會建置網站地圖,只會準備物件的狀態 SiteMapProvider 來執行此動作。 預設實作會 SecurityTrimmingEnabled 從網站導覽組態初始化網站地圖提供者的屬性。
衍生自 的 SiteMapProvider 類別可以覆寫 方法, Initialize 以初始化從永續性儲存體載入網站地圖資料所需的任何狀態和資源。 例如,如果您的衍生類別使用檔案來儲存網站地圖資料,則可以在 Initialize 方法中執行任何檔案初始化。 如果衍生類別使用某種其他類型的資料存放區,例如關係資料庫,可能會執行資料庫連接初始化。
ASP.NET 組態系統會讀取其他屬性,例如檔案名或連接字串,並使用其 NameValueCollection 參數傳遞至 Initialize 方法。
給繼承者的注意事項
在 Initialize(String, NameValueCollection) 衍生類別中覆寫 方法時,請務必先呼叫 Initialize(String, NameValueCollection) 基類的方法,再執行您自己的初始化。