SiteMapProvider.Initialize Method
Assembly: System.Web (in system.web.dll)
public void Initialize ( String name, NameValueCollection attributes )
public override function Initialize ( name : String, attributes : NameValueCollection )
Parameters
- name
The Name of the provider to initialize.
- attributes
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.
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 method in a derived class, be sure to first call the Initialize method for the base class before performing your own initializations.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 void Initialize(String name, NameValueCollection attributes)
throws Exception
{
if (get_IsInitialized())
{
return;
}
super.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.get_Item(
accessConnectionStringName);
if (null == connectionString || connectionString.get_Length() == 0)
{
throw new Exception("The connection string was not found.");
}
else
{
accessConnection = new OleDbConnection(connectionString);
}
initialized = true;
} //Initialize
Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.