需要在web.config中创建自定义配置节分两步走:
- 建立自己的配置节,下面是MSDN上的一个例子:
<myGroup>
<nestedGroup>
<mySection>
<add key="key_one" value="1"/>
<add key="key_two" value="2"/>
</mySection>
</nestedGroup>
</myGroup> - 然后在web.config中的<configSections><configSections>扇区中指明对自定义配置节进行操作的类。这个类可以是自定义的类,也可以是.NET Framework中已经有的类。无论怎样这样的类都必须实现IConfigurationSectionHandler 接口。这个接口中只有一个Create()方法。同样是MSDN上的例子,这里是用System.Configuration.NameValueSectionHandler这个类来进行处理。
<configSections>
<sectionGroup name="myGroup">
<sectionGroup name="nestedGroup">
<section name="mySection" type=
"System.Configuration.NameValueSectionHandler,System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
</sectionGroup>
</sectionGroup>
</configSections>
我们再来看看public class NameValueSectionHandler : IConfigurationSectionHandler,的确实现了IConfigurationSectionHandler接口。