Share via


SiteMapProvider.Initialize(String, NameValueCollection) メソッド

定義

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)

パラメーター

name
String

初期化するプロバイダーの Name

attributes
NameValueCollection

プロバイダーの初期化を支援する追加属性を含む NameValueCollection。 これらの属性は、Web.config ファイルのサイト マップ プロバイダー構成から読み取られます。

次のコード例は、メソッドをオーバーライド Initialize して Microsoft Access データベース接続を準備する方法を示しています。

オブジェクトのOleDbConnection接続文字列は、メソッドのNameValueCollectionInitializeパラメーターで渡されます。 この場合、接続文字列は、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) 実行する前に、まず基底クラスのメソッドを呼び出してください。

適用対象

こちらもご覧ください