一. 场景
    这里仅举一个简单应用的例子,我希望在web.config里面增加网站的基本信息,如:网站名称,网站版本号,是否将网站暂时关闭等。
二. 基本实现方法
1. 定义配置节点对应的类:SiteSetting
    代码片段:
    
namespace Tristan.SeeCustomConfig {
    public class SiteSetting {
        public string SiteName { get; set; }
        public string SiteVersion { get; set; }
        public bool Closed { get; set; }
    }
}

2. 实现IConfigurationSectionHandler接口:SiteSettingHandler

namespace Tristan.SeeCustomConfig {
    public class SiteSettingHandler : IConfigurationSectionHandler {
        #region IConfigurationSectionHandler Members

public object Create(object parent, object configContext, System.Xml.XmlNode section) {
            string siteName = section.SelectSingleNode("siteName").InnerText;
            string siteVersiton = section.SelectSingleNode("siteVersion").InnerText;
            bool closed = Convert.ToBoolean(section.SelectSingleNode("closed").InnerText);
            return new SiteSetting() { SiteName = siteName, SiteVersion = siteVersiton };
        }

#endregion
    }
}

3. 在web.config中进行配置
    在<configSections></configSections>里面增加一个节点:
<section name="siteSetting" type="Tristan.SeeCustomConfig.SiteSettingHandler"/>

name:指定我们将要增加的节点名为"siteSetting",接下来会使用它来编写配置节点
    type:指定处理这个配置节点的handler,这个类,我们在前面已经把代码实现了
    然后在<configuration><configuration>里面增加一段xml:
<siteSetting>
        <siteName>遇见未来</siteName>
        <siteVersion>1.0</siteVersion>
    <closed>false</closed>
    </siteSetting>

4. 看看效果吧
    随便建一个页面在后台代码里写几行代码做个测试:
    
namespace Tristan.SeeCustomConfig {
    public partial class _Default : System.Web.UI.Page {
        protected void Page_Load(object sender, EventArgs e) {
            SiteSetting site = ConfigurationManager.GetSection("siteSetting") as SiteSetting;
            Response.Write(site.SiteName + "," + site.SiteVersion + "," + site.Closed.ToString());
        }
    }
}

运行,可以看到,我们在web.config中的信息被write出来了。 :)
三. 使用XML反序列化
1. 修改SiteSetting
    
namespace Tristan.SeeCustomConfig {

[Serializable]
    [XmlRoot("siteSetting")]
    public class SiteSetting {
        [XmlElement("siteName",typeof(string))]
        public string SiteName { get; set; }

[XmlElement("siteVersion",typeof(string))]
        public string SiteVersion { get; set; }

[XmlElement("closed",typeof(Boolean))]
        public bool Closed { get; set; }
    }
}

2. 修改SiteSettingHandler

namespace Tristan.SeeCustomConfig {
    public class SiteSettingHandler : IConfigurationSectionHandler {
        #region IConfigurationSectionHandler Members

public object Create(object parent, object configContext, System.Xml.XmlNode section) {
            //string siteName = section.SelectSingleNode("siteName").InnerText;
            //string siteVersiton = section.SelectSingleNode("siteVersion").InnerText;
            //bool closed = Convert.ToBoolean(section.SelectSingleNode("closed").InnerText);
            //return new SiteSetting() { SiteName = siteName, SiteVersion = siteVersiton };

string typeName = ((XmlElement)section).GetAttribute("type");
            XmlSerializer xz = new XmlSerializer(Type.GetType(typeName));
            using (StringReader sr = new StringReader(section.OuterXml)) {
                return xz.Deserialize(sr);
            }
        }

#endregion
    }
}

3. 修改web.config中的配置
    
<siteSetting type="Tristan.SeeCustomConfig.SiteSetting">
        <siteName>遇见未来</siteName>
        <siteVersion>1.0</siteVersion>
    <closed>false</closed>
    </siteSetting>

4. 再来看看
    不修改测试代码,得到了一样的效果 :)

使用IConfigurationSectionHandler在web.config中增加自定义配置的更多相关文章

  1. [转]通过继承ConfigurationSection,在web.config中增加自定义配置

    本文转自:http://www.blue1000.com/bkhtml/2008-02/55810.htm 前几天写了一篇使用IConfigurationSectionHandler在web.conf ...

  2. App.config和Web.config配置文件的自定义配置节点

    前言 昨天修改代码发现了一个问题,由于自己要在WCF服务接口中添加了一个方法,那么在相应调用的地方进行更新服务就可以了,不料意外发生了,竟然无法更新.左查右查终于发现了问题.App.config配置文 ...

  3. 通过Web.config中的configSections配置自己系统的全局常量

    通过Web.config中的configSections配置自己系统的全局常量 随着系统的庞大,你的全局信息保存在appsitting里可能会比较乱,不如为模块写个自定义的全局常量吧 首先在Web.C ...

  4. 加密web.config中的邮件配置mailSettings

    加密: 在命令提示符下键入: aspnet_regiis -pef connectionStrings 要加密的web.config完整路经 演示样例:C:\Program Files (x86)\M ...

  5. 释放SQL Server占用的内存 .Net 读取xml UrlReWriter 在web.config中简单的配置

    释放SQL Server占用的内存   由于Sql Server对于系统内存的管理策略是有多少占多少,除非系统内存不够用了(大约到剩余内存为4M左右),Sql Server才会释放一点点内存.所以很多 ...

  6. .Net高级编程-自定义错误页 web.config中<customErrors>节点配置

    错误页 1.当页面发生错误的时候,ASP.Net会将错误信息展示出来(Sqlconnection的错误就能暴露连接字符串),这样一来不好看,二来泄露网站的内部实现信息,给网站带来安全隐患,因此需要定制 ...

  7. web.config中namespace的配置(针对页面中引用)

    1,在页面中使用强类型时: @model GZUAboutModel @using Nop.Admin.Models//命名空间(注意以下) 2,可以将命名空间提到web.config配置文件中去,此 ...

  8. ASP.NET,web.config 中SessionState的配置

    web Form 网页是基于HTTP的,它们没有状态, 这意味着它们不知道所有的请求是否来自同一台客户端计算机,网页是受到了破坏,以及是否得到了刷新,这样就可能造成信息的丢失. 于是, 状态管理就成了 ...

  9. spring中增加自定义配置支持

    spring.schemas 在使用spring时,我们会首先编写spring的配置文件,在配置文件中,我们除了使用基本的命名空间http://www.springframework.org/sche ...

随机推荐

  1. Ajax1

    一.Ajax是什么? 全称"Asynchronous JavaScript and XML"(异步JavaScript和XML),是指一种创建交互式网页应用的网页开发技术. AJA ...

  2. Versioned table in Netezza

    Problem One QC process need to obtain tables and their row counts in a database in Netezza. We use t ...

  3. POJ2186

    poj2186 popular cows   Every cow's dream is to become the most popular cow in the herd. In a herd of ...

  4. TopShelf框架创建Windows服务作为Remoting的宿主案例:

    1.创建服务 using System; using System.Collections.Generic; using System.Linq; using System.Text; using S ...

  5. JavaWeb 学习006-4个页面,5条sql语句(添加、查看、修改、删除)

    今天遇到的问题: 1. 在list页面上添加信息时候,跳转到doAdd页面后,点击保存按钮,能够跳转回list页面,但是新增的信息不能显示出来,就像是没有执行添加操作一样. 这是什么问题? ①是不是到 ...

  6. vertical-align 垂直居中

    基线:字母‘x’的底部: 中线:与基线的距离为小写字母x高度的一半(即0.5ex),而ex同font-size相关,大部分浏览器认为1ex = 0.5em(em同样也是相对单位,不是绝对单位),因此会 ...

  7. python 生成 xml文件 属性的顺序问题

    需求很奇葩. 文档示例 <ITEM key="username" eng="User Name" chn="用户名" val=&quo ...

  8. ssh学习小记

    ssh 为Secure SHell 的缩写.OpenSSH: ssh协议的开源实现.  SSH协议版本 v1: 基于CRC-32做MAC,不安全:man-in-middle v2:双方主机协议选择安全 ...

  9. tomcat(三)--基本安装配置

    0x01  JDK和Tomcat安装 到oracle官网下载jdk,当前下载的版本是Linux x64 jdk-8u101-linux-x64.tar.gz 到apache官网下载tomcat,当前最 ...

  10. 对ArrayList操作时报错java.util.ConcurrentModificationException null

    用iterator遍历集合时要注意的地方:不可以对iterator相关的地方做添加或删除操作.否则会报java.util.ConcurrentModificationException 例如如下代码: ...