使用IConfigurationSectionHandler在web.config中增加自定义配置
一. 场景
这里仅举一个简单应用的例子,我希望在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中增加自定义配置的更多相关文章
- [转]通过继承ConfigurationSection,在web.config中增加自定义配置
本文转自:http://www.blue1000.com/bkhtml/2008-02/55810.htm 前几天写了一篇使用IConfigurationSectionHandler在web.conf ...
- App.config和Web.config配置文件的自定义配置节点
前言 昨天修改代码发现了一个问题,由于自己要在WCF服务接口中添加了一个方法,那么在相应调用的地方进行更新服务就可以了,不料意外发生了,竟然无法更新.左查右查终于发现了问题.App.config配置文 ...
- 通过Web.config中的configSections配置自己系统的全局常量
通过Web.config中的configSections配置自己系统的全局常量 随着系统的庞大,你的全局信息保存在appsitting里可能会比较乱,不如为模块写个自定义的全局常量吧 首先在Web.C ...
- 加密web.config中的邮件配置mailSettings
加密: 在命令提示符下键入: aspnet_regiis -pef connectionStrings 要加密的web.config完整路经 演示样例:C:\Program Files (x86)\M ...
- 释放SQL Server占用的内存 .Net 读取xml UrlReWriter 在web.config中简单的配置
释放SQL Server占用的内存 由于Sql Server对于系统内存的管理策略是有多少占多少,除非系统内存不够用了(大约到剩余内存为4M左右),Sql Server才会释放一点点内存.所以很多 ...
- .Net高级编程-自定义错误页 web.config中<customErrors>节点配置
错误页 1.当页面发生错误的时候,ASP.Net会将错误信息展示出来(Sqlconnection的错误就能暴露连接字符串),这样一来不好看,二来泄露网站的内部实现信息,给网站带来安全隐患,因此需要定制 ...
- web.config中namespace的配置(针对页面中引用)
1,在页面中使用强类型时: @model GZUAboutModel @using Nop.Admin.Models//命名空间(注意以下) 2,可以将命名空间提到web.config配置文件中去,此 ...
- ASP.NET,web.config 中SessionState的配置
web Form 网页是基于HTTP的,它们没有状态, 这意味着它们不知道所有的请求是否来自同一台客户端计算机,网页是受到了破坏,以及是否得到了刷新,这样就可能造成信息的丢失. 于是, 状态管理就成了 ...
- spring中增加自定义配置支持
spring.schemas 在使用spring时,我们会首先编写spring的配置文件,在配置文件中,我们除了使用基本的命名空间http://www.springframework.org/sche ...
随机推荐
- linux随笔2
---恢复内容开始--- 2016,12,20 在linux里面不写这些扩展名,也是可以的,但是写这些扩展名是为了个管理员写的,好区分,但是linux本身不依靠扩展名来区分文件类型 ~~~~~~~~~ ...
- 循序渐进Python3(十)-- 1 -- pymysql
使用pymsql 模块操作数据库 #!/usr/bin/env python , ),()]), user='root', passwd='123456', db='test')# 创建游标curso ...
- Android开发环境
1: JDK 2: Eclipse 3: Android SDK 4: ADT
- spark job, stage ,task介绍。
1. spark 如何执行程序? 首先看下spark 的部署图: 节点类型有: 1. master 节点: 常驻master进程,负责管理全部worker节点. 2. worker 节点: 常驻wor ...
- 纯js异步无刷新请求(只支持IE)
纯js异步无刷新请求 下载地址:http://pan.baidu.com/s/1slakL1F 所以因为非IE浏览器都禁止跨域请求,所以以只支持IE. <HTML> <!-- 乱码( ...
- Linux下错误的捕获:全局变量errno和strerror()
经常在调用linux 系统api 的时候会出现一些错误,比方说使用open() write() creat()之类的函数有些时候会返回-1,也就是调用失败,这个时候往往需要知道失败的原因.这个时候使用 ...
- modelsim仿真vivado自动化脚本
quit -sim set PATH1 C:/modeltech64_10.2c/xilinx144_lib set PATH2 C:/xilinx1/Vivado/2014.4/data/veril ...
- ubuntu pip 安装django报错解决
系统版本 ubuntu Kylin 16.04 LTS 安装pip3 安装 Django 总是提示time out,无法安装. 逛了好多论坛终于遭到了解决办法,分享保存: sudo pi ...
- java_method_下拉框成json
List<String[]> proList=service.getUserList(); int nTotal=0; String proJson="["; proJ ...
- Java 枚举7种常见用法
(转)原创地址:http://blog.lichengwu.cn/java/2011/09/26/the-usage-of-enum-in-java/ JDK1.5引入了新的类型--枚举.在 Java ...