SiteMapProvider.Initialize(String, NameValueCollection) Methode

Definitie

Initialiseert de implementatie, inclusief alle resources die nodig zijn voor het SiteMapProvider laden van siteoverzichtgegevens uit permanente opslag.

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)

Parameters

name
String

De Name provider die moet worden geïnitialiseerd.

attributes
NameValueCollection

Een NameValueCollection die aanvullende kenmerken kan bevatten om de provider te initialiseren. Deze kenmerken worden gelezen uit de configuratie van de siteoverzichtprovider in het Web.config-bestand.

Voorbeelden

In het volgende codevoorbeeld ziet u hoe u de methode Initialize kunt overschrijven om een Microsoft Access databaseverbinding voor te bereiden.

De verbindingsreeks voor het object OleDbConnection wordt doorgegeven in de parameter NameValueCollection van de methode Initialize. In dit geval wordt de verbindingsreeks geleverd door de providerspecifieke sectie in het Web.config-bestand. Hier bevat accessSiteMapConnectionString een verbindingsreeks aan een Microsoft Access-database die als host fungeert voor de siteoverzichtgegevens.

<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>

Dit codevoorbeeld maakt deel uit van een groter voorbeeld voor de SiteMapProvider klasse.

   // 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

Opmerkingen

De Initialize methode bouwt geen siteoverzicht, maar bereidt alleen de status van het SiteMapProvider object voor. De standaard implementatie initialiseert de SecurityTrimmingEnabled eigenschap voor de siteoverzichtprovider vanuit de sitenavigatieconfiguratie.

Klassen die zijn afgeleid van de SiteMapProvider methode kunnen de Initialize methode overschrijven om alle statussen en resources te initialiseren die nodig zijn voor het laden van siteoverzichtsgegevens uit permanente opslag. Als uw afgeleide klasse bijvoorbeeld bestanden gebruikt om siteoverzichtgegevens op te slaan, kan elke bestandsinitialisatie in de Initialize methode worden uitgevoerd. Als de afgeleide klasse een ander type gegevensarchief gebruikt, zoals een relationele database, kan het initialiseren van een databaseverbinding worden uitgevoerd.

Aanvullende kenmerken, zoals bestandsnamen of verbindingsreeksen, worden gelezen door het ASP.NET configuratiesysteem en doorgegeven aan de methode Initialize met de parameter NameValueCollection.

Notities voor overnemers

Wanneer u de Initialize(String, NameValueCollection) methode in een afgeleide klasse overschrijft, moet u eerst de Initialize(String, NameValueCollection) methode voor de basisklasse aanroepen voordat u uw eigen initialisaties uitvoert.

Van toepassing op

Zie ook