使用IConfigurationSectionHandler在web.config中增加自定义配置
VS2008、ASP.NET 3.5
一. 场景
这里仅举一个简单应用的例子,我希望在web.config里面增加网站的基本信息,如:网站名称,网站版本号,是否将网站暂时关闭等。
二. 基本实现方法
1. 定义配置节点对应的类:SiteSetting
代码片段:
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 {
IConfigurationSectionHandler Members#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>里面增加一个节点:
name:指定我们将要增加的节点名为"siteSetting",接下来会使用它来编写配置节点
type:指定处理这个配置节点的handler,这个类,我们在前面已经把代码实现了
然后在<configuration><configuration>里面增加一段xml:
<siteName>遇见未来</siteName>
<siteVersion>1.0</siteVersion>
<closed>false</closed>
</siteSetting>
4. 看看效果吧
随便建一个页面在后台代码里写几行代码做个测试:
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
[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 {
IConfigurationSectionHandler Members#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中的配置
<siteName>遇见未来</siteName>
<siteVersion>1.0</siteVersion>
<closed>false</closed>
</siteSetting>
4. 再来看看
不修改测试代码,得到了一样的效果 :)
转载于:https://www.cnblogs.com/guozhijian/archive/2008/01/26/1054210.html
使用IConfigurationSectionHandler在web.config中增加自定义配置相关推荐
- Web.config中创建自定义配置节
Web.config文件可谓是每个ASP.NET站点必不可少的一个文件,其中记录了网站的各种配置.一些比较静态但是又可能变更的数据可以放在web.config中,已有的appSettings配置节便有 ...
- web.config中namespace的配置(针对页面中引用)
web.config中namespace的配置(针对页面中引用) 1,在页面中使用强类型时: @model GZUAboutModel @using Nop.Admin.Models//命名空间(注意 ...
- web.config 中SessionState的配置 [转]
一 http://hi.baidu.com/zyb512/blog/item/28ed858b3b88c015c9fc7af9.html web Form 网页是基于HTTP的,它们没有状态, 这意味 ...
- web.config中的session配置详解
打开某个应用程序的配置文件Web.config后,我们会发现以下这段: < sessionState mode="InProc" stateConnectionString= ...
- 在web.config中添加配置信息
1 在web.config中添加配置信息<configuration><connectionStrings><add name="conStr" co ...
- 在Web.config或App.config中的添加自定义配置
.Net中的System.Configuration命名空间为我们在web.config或者app.config中自定义配置提供了完美的支持.最近看到一些项目中还在自定义xml文件做程序的配置,所以忍 ...
- 在 Web 应用中增加用户跟踪功能
http://www.ibm.com/developerworks/cn/web/wa-lo-usertrack/index.html 进行用户跟踪的重要性 随着 Web 应用的复杂化,用户在网站上的 ...
- 解决打不开 RSA 密钥容器 即:加密web.config中的内容
简单的解决方法: WebConfig 加解密,未能使用提供程序"RsaProtectedConfigurationProvider"进行解密.提供程序返回错误消息为: 打不开 RS ...
- web.config文件之自定义错误节
web.config文件之自定义错误节 大家都知道,在开发asp.net应用程序时,通过web.config文件可以配置在执行 Web 请求期间发生未处理的错误时,ASP.NET 显示信息的方式.下面 ...
最新文章
- C++:Lambda函数学习
- iPhone7p与iPhoneX布局出现右边白边问题
- xss实例-输出在script/script之间的情况
- python mysql数据库操作grid控件_Python学习笔记_02:使用Tkinter连接MySQL数据库实现登陆注册功能...
- 位同步技术中的比相法
- Asp导出到Excel之二
- Java面试知识点:红黑树、Stream流
- win7怎么清理java缓存文件夹_win7系统清理电脑缓存文件的方法
- spring基础——外部引入属性文件创建bean
- vivo6.0系统怎么样不用root激活XPOSED框架的方法
- u-boot移植随笔:u-boot的内存分布图
- MYSQL 中varchar类型转text格式
- android root su sticky bit
- 计算机网络图标打不开怎么回事,双击打不开图标怎么办 双击打不开图标解决方法【详解】...
- 第四十九篇: JAVA加密解密之凯撒加密(Caesar cipher)算法
- java io broken pipe解决_报错!!java.io.IOException: 断开的管道 中文报错?Broken pipe?这次发生的报错不影响业务,已有解决方案...
- 如何在Server 2003查看 是 32位还是64位
- IG02数据准备(翻译)
- Vue 组件传值通信、父子组件、爷孙组件传值、方法调用
- 删除word文档中莫名其妙的空格。