在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