您的位置:首页 > 编程语言 > ASP

ASP.NET 实践:使用 IConfigurationSectionHandler 创建自定义配置段

2006-12-23 12:03 621 查看
你可以使用自定义 XML 配置元素对 ASP.NET 配置设定的标准集进行扩展。要这样做,你必须创建属于自己的配置段处理器。

配置段处理器必须是实现了
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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐