当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > 在.NET框架下使用自定义配置设置

ASP.NET
漫谈.Net开发关于命名空间和目录划分
.net中关于企业Excel报表的生成
.NET中取得IP/用户名等信息常用方法
关于.Net开发下的分布式缓存设计
.NET中为组合框添加自动查询功能
ASP.NET如何进行性能优化问题
开发中如何有效监控.NET应用程序
ASP.NET2.0 显示写入日期和时间语法
用.NET Array类的Sort方法分类数值
Asp.net Mvc Pv4中使用AjaxHelper
Asp.net中Forms验证的角色验证授权(一)
Asp.net中Forms验证的角色验证授权(二)
Asp.Net2.0数据库基本操作方法学习
Asp.Net之枚举类型输出需要类型转换
用.NET的File控件上传文件的解决方案
.NET泛型技巧之类型参数之间的转换
在ASP.NET中将数据直接输出成Excel格式
在.NET框架下使用自定义配置设置
跟ASP.NET MVC一起使用jQuery
Visual Basic中文本框处理技巧集萃

ASP.NET 中的 在.NET框架下使用自定义配置设置


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 102 ::
收藏到网摘: n/a

.NET框架通过基于XML的配置使配置设置驾轻就熟。它同时还提供了必要的方法,通过集合类(Collection classes)访问这些设置。

通过一个静态的ConfigurationSettings类可访问实际的配置数据。该类还提供了一个GetConfig()方法,可向一个合适的集合返回一个对象。本文中,我将示范三种可用来访问和存储配置信息的方法。

应用配置数据存储在App.config文件,并由configSections节点定义。每一section都有一个type属性定义。这里我将讨论的3个类型为NameValueSectionHandler、SingleTagSectionHandler和DictionarySectionHandler。你可以用一个sectionGroup元素定义节组。以下是一个配置节定义的例子:

<section name="MyCustomSection"
type="System.Configuration.NameValueSectionHandler"/>

建议使用type="System.Configuration.NameValueSectionHandler,System,Version=1.0.3300.0,Culture=neutral, PublicKeyToken=b77a5c561934e089"

节组是嵌入一个sectionGroup元素的独立配置节。以下是一个节组的例子:

最后,你所指定的配置节将用于构造存储配置数据的自定义的XML节点。若要向配置节添加数据,只要将该配置节作为一个XML节点包含进去,并用add节点添加Collection数据。下例为一个NameValueSectionHandler配置节:

以下为引用的内容:

<MyCustomSection>
<add key="key1" value="value1"/>
<add key="key2" value="value2"/>
</MyCustomSection>

MyCustomSection程序段包含一个命名值集合,其两个入口由key1和key2定义。

SingleTagSectionHandler较容易构造。正如NameValueSectionHandler,配置节可在configSections节点中找到。但在SingleTagSectionHandlers和NameValueSectionHandlers中,配置数据的添加方式是不同的,如下所示:

以下为引用的内容:

. . .
<section name="MySingleTagSection"
type="System.Configuration.SingleTagSectionHandler"/>
. . .
<MySingleTagSection setting1="value1" setting2="value2" setting3="value3"/>
. . .

DictionarySectionHandler与NameValueSectionHandler相似,但前者返回hashtable,后者返回NameValueCollection。当访问大量配置值时,hashtable要快于NameValueCollectio。DictionarySectionHandler与NameValueSectionHandler的构造方式相同,如下例:

. . .
<section name="MyDictionarySection"
type="System.Configuration.DictionarySectionHandler"/>
. . .
<MyDictionarySection>
<add key="key1" value="value1"/>
</MyDictionarySection>
. . .

我自己用了一下,报错。。原因还挺特别

System.Configuration.DictionarySectionHandler,System,Version=1.0.3300.0,Culture=neutral, PublicKeyToken=b77a5c561934e089

没有办法把type改为以上那段。终于行了。

构造节组的方法与构造单独配置节的方法基本相同,唯一的不同在于前者的自定义节点互相嵌套。借用前面的节组定义,以下是对节组的实现:

以下为引用的内容:

<CustomGroup>
<Custom1>
<add key="key1" value="value1"/>
</Custom1>
<Custom2>
<add key="key1" value="value1"/>
</Custom2>
</CustomGroup>

通过System.Configuration.ConfigurationSettings命名空间的GetConfig()方法和自定义配置节的串值来访问应用配置设置,然后将该方法的结果转为合适的类型。

对于SingleTagSectionHandler,将结果转为System.Collections命名空间的IDictionary接口类型。对于NameValueSectionHandler,结果转为在System.Collections.Specialized命名空间中定义的NameValueCollection类型。最后,对于DictionarySectionHandler,结果转为System.Collections命名空间中的Hashtable类型。

对于节组,唯一的区别是,将加上正斜杠和配置节名的节组名作为字符串参数传递给GetConfig()方法,以访问自定义设置。

以下是一个使用这些自定义设置的实例:

以下为引用的内容:

    System.Collections.IDictionary stsh = (System.Collections.IDictionary)
System.Configuration.ConfigurationSettings.GetConfig("MySingleTagSection");
    System.Collections.Specialized.NameValueCollection nvsh =
 (System.Collections.Specialized.NameValueCollection)
 System.Configuration.ConfigurationSettings.GetConfig("MyNameValueSection");
    System.Collections.Hashtable dsh = (System.Collections.Hashtable)
 System.Configuration.ConfigurationSettings.GetConfig("MyDictionarySection");
    System.Collections.Specialized.NameValueCollection sgnvsh =
 (System.Collections.Specialized.NameValueCollection)
 System.Configuration.ConfigurationSettings.GetConfig("MySectionGroup/MySection
1");
    System.Diagnostics.Debug.WriteLine((string)stsh["sample1"]);
    System.Diagnostics.Debug.WriteLine((string)nvsh["key1"]);
    System.Diagnostics.Debug.WriteLine((string)dsh["key1"]);
    System.Diagnostics.Debug.WriteLine((string)sgnvsh["key1"]);

以下是用于上面代码的一段配置XML代码:

以下为引用的内容:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="MySingleTagSection"
 type="System.Configuration.SingleTagSectionHandler"/>
        <section name="MyDictionarySection"
 type="System.Configuration.DictionarySectionHandler"/>
        <section name="MyNameValueSection"
type="System.Configuration.NameValueSectionHandler"/>
        <sectionGroup name="MySectionGroup">
            <section name="MySection1"
 type="System.Configuration.NameValueSectionHandler"/>
            <section name="MySection2"
 type="System.Configuration.NameValueSectionHandler"/>
        </sectionGroup>
    </configSections>
    <MySingleTagSection sample1="value1" sample2="value2" sample3="value3"/>
    <MyDictionarySection>
        <add key="key1" value="value1"/>
        <add key="key2" value="value2"/>
    </MyDictionarySection>
    <MyNameValueSection>   
        <add key="key1" value="value1"/>
        <add key="key2" value="value2"/>
    </MyNameValueSection>
    <MySectionGroup>
        <MySection1>
            <add key="key1" value="value1"/>
            <add key="key2" value="value2"/>
        </MySection1>
        <MySection2>
            <add key="key1" value="value1"/>
            <add key="key2" value="value2"/>
        </MySection2>
    </MySectionGroup>
</configuration>