ASP.NET 实践:使用 IConfigurationSectionHandler 创建自定义配置段
2006-12-23 12:03
621 查看
你可以使用自定义 XML 配置元素对 ASP.NET 配置设定的标准集进行扩展。要这样做,你必须创建属于自己的配置段处理器。
配置段处理器必须是实现了
提示:该文章使用
配置段处理器对定义在 Web.config 文件特定位置的 XML 配置元素设定进行定义和处理,并且返回一个适当的配置对象,该对象基于配置设定。配置段处理器类的配置对象可以返回任何一种数据结构;可见并没有对任何一种配置基类或配置格式进行限制。ASP.NET 使用配置对象对你的自定义配置元素进行读取和写入。
添加你自己的代码来完成所需要的配置操作。
例如,你可以对
提示:
当你为定义在更高级别配置文件层次的配置段处理器添加自定义配置元素时,你可以把配置段处理器定义添加到不同的文件中。
在你的 Web.config 文件的配置段设定区中添加你的自定义配置元素。
下例 ASPX 页面实例与前面的实例一起工作,并对自定义配置段的参数集和子元素集进行列举。因为自定义配置段处理器使用
配置段处理器必须是实现了
System.Configuration.IConfigurationSectionHandler接口或
System.Configuration.ConfigurationSection类的 .NET Framework 类。
提示:该文章使用
System.Configuration.IConfigurationSectionHandler接口,但是该接口在 .NET Framework 2.0 中不再继续使用。
配置段处理器对定义在 Web.config 文件特定位置的 XML 配置元素设定进行定义和处理,并且返回一个适当的配置对象,该对象基于配置设定。配置段处理器类的配置对象可以返回任何一种数据结构;可见并没有对任何一种配置基类或配置格式进行限制。ASP.NET 使用配置对象对你的自定义配置元素进行读取和写入。
创建自定义配置段处理器
创建实现System.Configuration.IConfigurationSectionHandler接口的公共类,如下例代码所示。
using System; using System.Collections.Generic; using System.Text; using System.Configuration; using System.Xml; namespace MyConfigSectionHandler { public class MyHandler : IConfigurationSectionHandler { #region IConfigurationSectionHandler 的成员 object IConfigurationSectionHandler.Create( object parent, object configContext, XmlNode section) { throw new Exception("该方法未被实现。"); } #endregion } }
添加你自己的代码来完成所需要的配置操作。
例如,你可以对
throw new Exception("The method is not implemented.");进行覆盖;这行代码从
section参数的
Attributes集合中获取参数名称和值,然后返回一个自定义配置对象。下例实例使用了
Hashtable作为配置对象。
using System; using System.Collections; using System.Text; using System.Configuration; using System.Xml; namespace MyConfigSectionHandler { public class MyHandler : IConfigurationSectionHandler { #region IConfigurationSectionHandler 的成员 object IConfigurationSectionHandler.Create( object parent, object configContext, XmlNode section) { // 该方法会返回被创建的配置对象。 // 可以是一个自定义配置类。 // 在该实例中,我们使用了一个 System.Collections.Hashtable。 Hashtable myConfigObject = new Hashtable(); // 获取该配置段元素参数集中的任何一个参数。 Hashtable myAttribs = new Hashtable(); foreach (XmlAttribute attrib in section.Attributes) { if (XmlNodeType.Attribute == attrib.NodeType) myAttribs.Add(attrib.Name, attrib.Value); } // 把配置段的名称和参数集作为第一个配置对象项。 myConfigObject.Add(section.Name, myAttribs); // 获取子元素的名称和参数集。 foreach (XmlNode child in section.ChildNodes) { if (XmlNodeType.Element == child.NodeType) { Hashtable myChildAttribs = new Hashtable(); foreach (XmlAttribute childAttrib in child.Attributes) { if (XmlNodeType.Attribute == childAttrib.NodeType) myChildAttribs.Add(childAttrib.Name, childAttrib.Value); } myConfigObject.Add(child.Name, myChildAttribs); } } return (myConfigObject); } #endregion } }
在 ASP.NET 配置文件中添加自定义配置段处理器
在你的 Web.config 文件的configSections元素中添加
sectionGroup和
section元素,如下例代码所示。
提示:
sectionGroup中嵌套的
section元素是可选的,但是它不利于配置数据的良好组织。
当你为定义在更高级别配置文件层次的配置段处理器添加自定义配置元素时,你可以把配置段处理器定义添加到不同的文件中。
section元素的
type参数必须与汇编集清单相匹配,不然会发生配置错误。汇编集文件自身必须与定义它的 Web.config 文件位于相同的 ASP.NET 应用程序目录中。
<configuration> <!-- 配置段处理器的声明区。 --> <configSections> <sectionGroup name="myCustomGroup"> <section name="myCustomSection" type="MyConfigSectionHandler.MyHandler, MyCustomConfigurationHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" allowLocation="true" allowDefinition="Everywhere" /> </sectionGroup> <!-- 其他的 <section> 和 <sectionGroup> 元素 --> </configSections> <!-- 配置段的设定区。 --> </configuration>
在你的 Web.config 文件的配置段设定区中添加你的自定义配置元素。
<configuration> <!-- 配置段处理器的声明区。 --> <!-- 配置段的设定区。 --> <myCustomGroup> <myCustomSection myAttrib1="Clowns"> <myChildSection myChildAttrib1="Zippy" myChildAttrib2="Michael Zawondy "/> </myCustomSection> </myCustomGroup> <!-- 其他配置的设定,例如 <system.web> --> </configuration>
编程访问你的自定义配置数据
使用System.Configuration.ConfigurationManager.GetSection(System.String)方法或
System.Web.Configuration.WebConfigurationManager.GetSection(System.String)方法获得一个你的自定义配置对象实例并进行操作。
下例 ASPX 页面实例与前面的实例一起工作,并对自定义配置段的参数集和子元素集进行列举。因为自定义配置段处理器使用
Hashtable作为配置对象,所以
GetSection方法会把该
Hashtable对象作为返回值。
<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> protected void Button1_Click(object sender, EventArgs e) { Hashtable config = (Hashtable)System.Configuration.ConfigurationManager.GetSection("myCustomGroup/myCustomSection"); StringBuilder sb = new StringBuilder(); sb.AppendFormat("配置对象的个数 = {0}<br/><br/>", config.Count); foreach (DictionaryEntry deKey in config) { sb.AppendFormat("<h2>第 {0} 个元素的参数集:</h2>", deKey.Key.ToString()); Hashtable attribs = (Hashtable)deKey.Value; foreach (DictionaryEntry deAttrib in attribs) { sb.AppendFormat("{0} = {1}<br/>", deAttrib.Key.ToString(), deAttrib.Value.ToString()); } } Label1.Text = sb.ToString(); Label1.Visible = true; } </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>无标题页面</title> </head> <body> <form id="form1" runat="server"> <div> <h1>列举 MyCustomSection</h1> <asp:Label ID="Label1" runat="server" Text="" /> <br /> <asp:Button ID="Button1" runat="server" Text="获取自定义配置信息" OnClick="Button1_Click" /> </div> </form> </body> </html>
相关文章推荐
- ASP.NET 实践:使用 ConfigurationSection 创建自定义配置段
- 使用IConfigurationSectionHandler在web.config中增加自定义配置
- 使用IConfigurationSectionHandler在web.config中增加自定义配置
- 使用IConfigurationSectionHandler在web.config中增加自定义配置
- 使用IConfigurationSectionHandler在web.config中增加自定义配置
- 使用IConfigurationSectionHandler在web.config中增加自定义配置
- 使用IConfigurationSectionHandler在web.config中增加自定义配置
- 使用IConfigurationSectionHandler在web.config中增加自定义配置
- 使用IConfigurationSectionHandler在web.config中增加自定义配置
- [dotnetCore2.0]学习笔记之二: ASP.NET Core中,如何灵活使用静态文件和加载自定义配置
- 使用 Visual Basic .NET 在 ASP.NET 中创建自定义的错误报告页
- HOW TO:使用 Visual Basic .NET 在 ASP.NET 中创建自定义的错误报告页
- HOW TO:使用 Visual C# .NET 在 ASP.NET 中创建自定义错误报告
- HOW TO:使用 Visual C# .NET 在 ASP.NET 中创建自定义错误报告
- 在ASP.NET中使用自定义配置
- 使用 Visual C# .NET 在 ASP.NET 中创建自定义错误报告
- HOW TO:使用 Visual Basic .NET 在 ASP.NET 中创建自定义的错误报告页
- ASP.NET MVC 实践之路 之四 使用View来创建页面
- (学习Asp.net Ajax笔记一)使用 Microsoft AJAX Library 创建自定义客户端脚本之一
- IT咨询顾问:一次吐血的项目救火 java或判断优化小技巧 asp.net core Session的测试使用心得 【.NET架构】BIM软件架构02:Web管控平台后台架构 NetCore入门篇:(十一)NetCore项目读取配置文件appsettings.json 使用LINQ生成Where的SQL语句 js_jquery_创建cookie有效期问题_时区问题