您可以用自己的 XML 配置标记扩展标准的 ASP.NET 配置设置集。若要完成该操作,您必须创建自己的配置节处理程序。该处理程序必须是一个实现 IConfigurationSectionHandler 接口的 .NET Framework 类。节处理程序解释并处理 Web.config 文件特定部分中 XML 标记中定义的设置并根据配置设置返回适当的配置对象。处理程序类返回的配置对象可以是任何数据结构;它不限于任何基配置类或配置格式。
下面的示例定义 IConfigurationSectionHandler 接口。
[Visual Basic]
Namespace System.Web.Configuration
Public Interface IConfigurationSectionHandler
Function Create(parent As Object, input As Object, _
node As XmlNode) As Object
End Interface
End Namespace
[C#]
namespace System.Web.Configuration
{
public interface IConfigurationSectionHandler
{
public Object Create(Object parent, Object input,
XmlNode node);
}
}
您还可以定义自己的节,该节与 <appSettings> 节使用相同的配置处理程序。例如:
<configuration>
<configSections>
<sectionGroup name="myGroup">
<sectionGroup name="nestedGroup">
<section name="mySection" type=
"System.Configuration.NameValueSectionHandler,System"/>
</sectionGroup>
</sectionGroup>
</configSections>
<myGroup>
<nestedGroup>
<mySection>
<add key="key_one" value="1"/>
<add key="key_two" value="2"/>
</mySection>
</nestedGroup>
</myGroup>
</configuration>
您可以读取上面的示例中定义的新配置节的值,如下:
[Visual Basic]
Dim config As NameValueCollection =
ConfigurationSettings.GetConfig("myGroup/nestedGroup/mySection")
Response.Write("The value of key_one is " & config("key_one") & "<br>")
Response.Write("The value of key_two is " & config("key_two") & "<br>")
[C#]
NameValueCollection config = (NameValueCollection)
ConfigurationSettings.GetConfig("myGroup/nestedGroup/mySection");
Response.Write("The value of key_one is " + config["key_one"] + "<br>");
Response.Write("The value of key_two is " + config["key_two"] + "<br>");
<system.net>
<! — Net Class Settings would go here. -->
</system.net>
<system.web>
<authorization>
<allow users="*"/> <!-- Allow all users -->
<!-- Allow or deny specific users.
allow users="[comma separated list of users]"
roles="[comma separated list of roles]"/>
<deny users="[comma separated list of users]"
roles="[comma separated list of roles]"/>
-->
</authorization>
<sessionState
sqlConnectionString="data source=localhost;
Integrated Security=SSPI;
Initial Catalog=northwind"
cookieless="false"
timeout="10"/>
</system.web>
</configuration>
ASP.NET 配置基础结构不对基础结构支持的配置数据的类型作出任何假设。配置节处理程序类处理所有 Web.config 数据。您可以使用与 .NET Framework 一起提供的预定义的配置节处理程序,或者您可以创建自己的处理程序来处理自己的自定义配置数据。