Redigera

Dela via


SiteMapProvider.Initialize(String, NameValueCollection) Method

Definition

Initializes the SiteMapProvider implementation, including any resources that are needed to load site map data from persistent storage.

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

The Name of the provider to initialize.

attributes
NameValueCollection

A NameValueCollection that can contain additional attributes to help initialize the provider. These attributes are read from the site map provider configuration in the Web.config file.

Examples

The following code example demonstrates how to override the Initialize method to prepare a Microsoft Access database connection.

The connection string for the OleDbConnection object is passed in the NameValueCollection parameter of the Initialize method. In this case, the connection string is provided by the provider-specific section in the Web.config file. Here, accessSiteMapConnectionString contains a connection string to a Microsoft Access database that hosts the site map data.

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

This code example is part of a larger example provided for the SiteMapProvider class.

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

Remarks

The Initialize method does not actually build a site map, it only prepares the state of the SiteMapProvider object to do so. The default implementation initializes the SecurityTrimmingEnabled property for the site map provider from the site navigation configuration.

Classes that derive from the SiteMapProvider can override the Initialize method to initialize any state and resources that are required to load site map data from persistent storage. For example, if your derived class is using files to store site map data, any file initialization can be performed in the Initialize method. If the derived class uses some other type of data store, such as a relational database, initializing a database connection might be performed.

Additional attributes, such as file names or connection strings, are read by the ASP.NET configuration system and passed to the Initialize method with its NameValueCollection parameter.

Notes to Inheritors

When overriding the Initialize(String, NameValueCollection) method in a derived class, be sure to first call the Initialize(String, NameValueCollection) method for the base class before performing your own initializations.

Applies to

See also