Sdílet prostřednictvím


SiteMapProvider.Initialize(String, NameValueCollection) Metoda

Definice

Inicializuje implementaci SiteMapProvider , včetně všech prostředků potřebných k načtení dat mapy webu z trvalého úložiště.

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)

Parametry

name
String

Inicializace Name poskytovatele

attributes
NameValueCollection

A NameValueCollection , který může obsahovat další atributy, které pomáhají inicializovat poskytovatele. Tyto atributy se čtou z konfigurace zprostředkovatele mapy webu v souboru Web.config.

Příklady

Následující příklad kódu ukazuje, jak přepsat metodu Initialize pro přípravu připojení databáze Aplikace Microsoft Access.

Připojovací řetězec objektu OleDbConnection se předává v NameValueCollection parametru Initialize metody. V tomto případě připojovací řetězec poskytuje oddíl specifický pro poskytovatele v souboru Web.config. Tady obsahuje připojovací řetězec k databázi Microsoft Accessu, accessSiteMapConnectionString která je hostitelem dat mapy webu.

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

Tento příklad kódu je součástí většího příkladu uvedeného pro třídu 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

Poznámky

Metoda Initialize ve skutečnosti nevystaví mapu webu, připraví pouze stav objektu SiteMapProvider k tomu. Výchozí implementace inicializuje SecurityTrimmingEnabled vlastnost poskytovatele mapy webu z konfigurace navigace webu.

Třídy, které jsou odvozeny z SiteMapProvider mohou přepsat metodu Initialize inicializace všech stavů a prostředků, které jsou nutné k načtení dat map webu z trvalého úložiště. Pokud například vaše odvozená třída používá soubory k ukládání dat mapy webu, lze v Initialize metodě provést inicializaci souborů. Pokud odvozená třída používá jiný typ úložiště dat, jako je relační databáze, může být inicializace připojení k databázi provedena.

Další atributy, jako jsou názvy souborů nebo připojovací řetězce, čtou konfigurační systém ASP.NET a předávají Initialize metodě s jeho NameValueCollection parametrem.

Poznámky pro dědice

Při přepsání Initialize(String, NameValueCollection) metody v odvozené třídě nezapomeňte před provedením vlastních inicializací nejprve volat Initialize(String, NameValueCollection) metodu pro základní třídu.

Platí pro

Viz také