在asp.net中,配置資料存放區在web.config檔案中。該檔案使用xml來表示資料,所有的配置資訊都位於和根xml標記之間。這裡的配置資訊分為兩個地區:配置節處理常式聲明地區、配置節設定地區。
配置節處理常式聲明地區位於和xml標記之間,使用section元素來聲明配置節處理常式,可以將這些配置節處理常式聲明嵌套在sectionGroup元素中,協助組織配置資訊,如下所示:
configSections> sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"> sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"> section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" /> sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"> section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere" /> section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" /> section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" /> section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" /> sectionGroup> sectionGroup> sectionGroup> configSections>
配置節處理常式聲明地區中的每個配置節都有一個節處理常式聲明,節處理常式是實現了ConfigurationSection類的.Net Framework類。節處理常式聲明中包含節的名稱(name)以及用來處理該節中配置資料的節處理常式類的名稱(type)。如下所示:
section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere" />
當您需要自訂配置節時,需要完成這樣幾個任務:
- 設計自己的配置節處理常式類,實現ConfigurationSection類,並擴充自己所需的功能;
- 在配置節處理常式聲明地區中聲明節處理常式類;
- 在配置節配置自訂的配置節。
1.實現ConfigurationSection類,並擴充自己所需的功能
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Configuration;namespace EveryDayStudy{ public class CustomSection : ConfigurationSection { public CustomSection() { } [ConfigurationProperty("CustomAttribute")] public string CustomAttribute { get { return (String)this["CustomAttribute"]; } set { this["CustomAttribute"] = value; } } }}
在這裡只添加了一個屬性 CustomAttribute .
通過對類的一個一般字元串屬性(Property)CustomAttribute進行修飾,使之成為了一個配置節的節屬性(Attribute),這是通過ConfigurationPropertyAttribute來實現的。
2、在配置節處理常式聲明地區中聲明節處理常式類
configSections> section name="mySections" type="EveryDayStudy.CustomSection"/> configSections>
3、在配置節配置自訂的配置節。
mySections CustomAttribute="MyCustomAttribute">mySections>
完成。下面我們在.CS檔案中運行看看吧。
CustomSection mySection = (CustomSection)ConfigurationManager.GetSection("mySections");Response.Write("CustomAttribute Value Is " + mySection.CustomAttribute);
運行。看看效果。
CustomAttribute Value Is MyCustomAttribute
現在,mySections元素僅僅是一個元素,如果我們希望newSection元素可以包含子項目呢?如下所示:
mySections CustomAttribute="MyCustomAttribute"> SubElement ElementAttribute="MyElementAttribute">SubElement>mySections>
要能夠給mySections元素添加一個子項目,需要對我們的自訂配置節程式類進行改進,如下所示:
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Configuration;namespace EveryDayStudy{ public class CustomSection : ConfigurationSection { public CustomSection() { } [ConfigurationProperty("CustomAttribute")] public string CustomAttribute { get { return (String)this["CustomAttribute"]; } set { this["CustomAttribute"] = value; } } [ConfigurationProperty("SubElement")] public CustomElement SubElement { //新添加的屬性,因為其傳回型別繼承自ConfigurationElement, //故可以在web.config中作為配置節的子項目使用。 get { return (CustomElement)this["SubElement"]; } set { this["SubElement"] = value; } } } public class CustomElement : ConfigurationElement { //新添加的自訂元素 [ConfigurationProperty("ElementAttribute")] public string ElementAttribute { get { return (String)this["ElementAttribute"]; } set { this["ElementAttribute"] = value; } } }}
在.CS檔案中訪問:
CustomSection mySection = (CustomSection)ConfigurationManager.GetSection("mySections"); Response.Write("CustomAttribute Value Is " + mySection.CustomAttribute); Response.Write("ElementAttribute Value Is " + mySection.SubElement.ElementAttribute);
運行看看效果:
CustomAttribute Value Is MyCustomAttribute
ElementAttribute Value Is MyElementAttribute
參考原文