使用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 ...
随机推荐
- iOS调试
iOS高效调试 写代码难免出现bug.储备些调试技能绝对能够提高你的工作效率,让bug无所遁形.下面就和大家分享一些我在工作中常用的iOS调试小技能. 1. 打印 最简单,基础的调试方法就是打印日志了 ...
- 将C#datagridview控件的数据导出到Excel中
1.添加引用Microsoft.Office.Interop.Excel. 2.程序代码引用using Excel = Microsoft.Office.Interop.Excel; 3.控件事件代码 ...
- LoadLibrary函数定位DLL顺序
用LoadLibrary此函数来加载动态链接库到内存,Window 定位DLL的搜寻路径如下: 当前进程的可执行模块所在的目录. 当前目录. Windows 系统目录.GetSystemDirecto ...
- [知识整理]Java集合(一) - List
一.实现List的几个类: ArrayList.LinkedList.CopyOnWriteArrayList.Vector 二.几个List底层的数据结构: ArrayList - 数组列表 Lin ...
- 【Python全栈笔记】08 [模块二] 20 Oct 递归 -*** 待补充
递归 引入 递归的表现形式 下面是四个函数,互相调用返回结果 # 引入 递归的表现形式 def f1(): ' def f2(): r = f1() return r def f3(): r = f2 ...
- String类的常用判断方法使用练习
选取了一些常用的判断方法进行了使用练习,后续跟新其他方法 package StringDemo; // String类的判断方法解析 // 1:boolean equals(); // 判断字符串是否 ...
- JavaScript 数组冒泡排序练习
12.29下午主要讲的是简单的一维数组 和数组中利用冒泡排序排列大小 比如有 数字 0.5 20 1 5 4 3 6 利用冒泡排序按照从小到大的顺序排列 var arr=ne ...
- ANDROID开发之问题积累及解决方案(二)
错误:“Binary XML file line # : Error inflating class” 原因:此异常是布局文件中有错引起的,那么返回到布局文件中看看. <?xml version ...
- git 常用操作
查看某文件的某些行的变化历史: $ git log --pretty=short -u -L 2003,2005:Executor.cpp http://stackoverflow.com/quest ...
- database link远程链接数据库
--授权创建.删除dblink GRANT CREATE [PUBLIC] DATABASE LINK,DROP [PUBLIC] DATABASE LINK TO canco; --查看数据库GLO ...