SiteMapProvider.Initialize(String, NameValueCollection) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
初始化 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)
参数
- attributes
- NameValueCollection
NameValueCollection,其中可以包含附加特性以帮助初始化提供程序。 从 Web.config 文件中的站点地图提供程序配置读取这些特性。
示例
下面的代码示例演示如何重写 Initialize 方法以准备 Microsoft Access 数据库连接。
对象的OleDbConnection连接字符串在方法的参数Initialize中NameValueCollection传递。 在这种情况下,连接字符串由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) 基类的方法。