zoukankan      html  css  js  c++  java
  • 自定义配置节处理实现个性化web.config

    通过System.Configuration中的ConfigurationSettings类的AppSettings属性,可以很方便访问web.config配置文件中appSettings节点的数据。对于web程序利用这个配置文件存放一些只读的程序信息,比如程序名称,作者信息,数据库连接字符串等将是十分方便有用。如:

    <!--Sample.Aspx-->
    private void Page_Load(object sender, System.EventArgs e)
    {
    this.tbName = ConfigurationSettings.AppSettings[“AppName“];
    }

    <!--web.config-->
    <configuration>
    <appSettings>
    <add key="AppName" value="MyApplication" />
    </appSettings>
    ... ...

    对于ConfigurationSettings类有个方法GetConfig(string sectionName)可以访问任何配置元素,对于以上例子,可如此使用:

    <!--Sample.Aspx-->
    private void Page_Load(object sender, System.EventArgs e)
    {
    object settings = ConfigurationSettings.GetConfig(“appSettings“);
    NameValueCollection nvc = settings as NameValueCollection;
    if (nvc != null)
    {
    string val = (string)nvc[“AppName“];
    this.tbName = val;
    }
    }

    可见GetConfig()方法返回了一个配置处理的对象,转换成NameValueCollection的实例后,可以访问到该section内的内容了。其实对于配置文件检索有背后的处理程序实现,同时我们可以看到在web.config,或machine.config中看到对于处理程序的声明,如:

    <!--web.config-->
    <configuration>
    <configSections>
    <section name="mySection"
    type="Chagel.Configration.Settings, Configuration" />
    </configSections>
    <mySection>
    <AppName> MyApplication</AppName>
    </mySection>
    ... ...

    以上声明了一个mySection元素,并在configSections中声明了该配置的处理程序类名为Chagel.Configration.Settings,Configuration为程序集名称。接下来我们可以通过一个实现System.Configuration.IConfigurationSectionHandler接口的类来处理该配置元素,如:

    <!--Settings.cs-->
    using Chagel.Configration.Data;
    namespace Chagel.Configration
    {
    public class Settings:IConfigurationSectionHandler
    { //实现该接口的Create方法
    public object Create(object parent, object input, XmlNode node)
    {
    Data data = new Data();

    foreach(XmlNode xn in node.ChildNodes)
    {
    switch(xn.Name)
    {
    case("appName"):
    data.AppName = xn.InnerText;
    break;
    case("appVer"):
    data.AppVer = xn.InnerText;
    break;
    ... ...
    }//switch end

    }//foreach end

    return data;

    }//method end
    }
    }

    IConfigurationSectionHandler 接口只有一种方法,每当发现注册到处理程序的配置节时,都会在节处理程序上调用 Create 方法,我们实现的类返回一个Data类的实例,该类是一个专门的数据集,代码如下:

    <!--Data.cs-->
    namespace Chagel.Configration.Data
    {
    public class Data
    {
    public Data()
    {
    }
    public string AppName;//程序名称
    public string AppVer;//程序版本
    public string AppAuthor;//程序作者
    ... ...
    }
    }

    至此,现在可以读取配置元素值了,如:

    <!--Sample1.Aspx-->
    private void Page_Load(object sender, System.EventArgs e)
    {
    Data data;
    data = ConfigurationSettings.GetConfig("mySection") as Data;
    this.tbName.Text = data.AppName;
    }

    到此我们通过实现一个类支持 IConfigurationSectionHandler 接口来对自定义节进行处理,完成对自定义节的读取。当然我们仍可以直接声明系统的处理程序(System.Configuration.NameValueFileSectionHandler)重用与appSettings一样的类。
  • 相关阅读:
    解决Cell重绘导致 重复的问题
    给Cell间隔颜色
    NSUserDefault 保存自定义对象
    xcode6 下载
    unrecognized selector sent to instance
    16进制颜色转换
    local unversioned, incoming add upon update问题
    应用崩溃邮件通知
    TabBar变透明
    代码手写UI,xib和StoryBoard间的博弈,以及Interface Builder的一些小技巧
  • 原文地址:https://www.cnblogs.com/jasononline/p/770898.html
Copyright © 2011-2022 走看看