一. 场景
    这里仅举一个简单应用的例子,我希望在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. cf732f

    思路:先缩点,再以最大连同分量为根dfs,代码太垃圾不想贴

  2. Excel表格解析

    //add by yangwenpei WGCW-144 使用Excel表格导入纸票记录 20161212 start /** * @param fileInputStream * @param co ...

  3. OpenGL ES crash notes 01 - Nice to meet you

    这篇笔记完全参照<OpenGL.ES.3.0.Programming.Guide.2nd.Edition>,摘出部分内容只为学习参考. 为什么要用英文:无论是D3D的SDK还是OES的Sp ...

  4. iOS 沙盒(sandbox)结构 使用 实例

    声明:该文档是经过自己查找网上的资料以及自己多年的经验后而总结出来的,希望对大家有所帮助,有什么不恰当支出还请大家多指点! iOS中的沙盒机制(SandBox)是一种安全体系,它规定了应用程序只能在为 ...

  5. NLP学术组织、会与论文

    1. 自然语言处理怎么最快入门? 2. 初学者如何查阅自然语言处理(NLP)领域学术资料 2.0  ACL Anthology 2.1  Association for Computational L ...

  6. memcached+magent实现memcached集群

    首先说明下memcached存在如下问题 本身没有内置分布式功能,无法实现使用多台Memcache服务器来存储不同的数据,最大程度的使用相同的资源:无法同步数据,容易造成单点故障.(memagent代 ...

  7. 遇到IIS7配置PHP出现403和404错误的解决办法

    服务器要配置PHP,总是出现403错误.服务器是新装的,操作系统是windows server 2008 R2,装的IIS7. IIS里PHP和本地服务器对比了好几遍,都没到出错的原因,后来通过cmd ...

  8. svn安装与其服务器搭建

    1.概述:SVN为程序开发团队常用的代码管理,版本控制软件. 2.工具: 1) TortoiseSVN-1.8.4.24972-win32-svn-1.8.5.msi  SVN安装包. 2)setup ...

  9. [PHP]OOP两类写法的性能对比

    在PHP的OOP中我们有常见两种方法调用,对象调用和静态调用. 下面是一个简单的测试来比较它们的细微差异. /** * 对象初始化 -> 调用:objectCall.php * * 测试调用50 ...

  10. Reveal分析IOS界面,plist文件读取

    Reveal分析IOS界面,需要得到app的 softwareVersionBundleId上传到iphone中 , 而IOS8的iTunesMetadata.plist (设备路径/var/mobi ...