为asp.net程序添加自定义配置区域 (转)
2005-03-14 08:19
525 查看
我们通常把诸如sql的connection string之类的配置信息保存在web.config的AppSettings部分,以方便程序的分发,并且可以通过以下方法在程序中获得:
string sqlStr = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
对于结构比较复杂的自定义配置,可以通过实现IConfigurationSectionHandler接口来实现这种机制。首先,创建MySettings类,该类仅包含了我需要的一些自定义配置的定义:
using System;
namespace myconfig
using System;
using System.Configuration;
using System.Xml;
namespace myconfig
<configSections>
<section name="MySettings" type="myconfig.MyConfigHandler,myconfig"></section>
</configSections>
<MySettings>
<SomeSetting>This is a customer configuration setting.</SomeSetting>
</MySettings>
其中<configSecions>告诉web.config调用MyConfigHandler来处理MySettings,<MySettings>中保存的就是自定义的配置内容,例如在某个web page中执行如下代码:
private void Page_Load(object sender, System.EventArgs e)
// Put user code to initialize the page here
MySettings myset;
myset = System.Configuration.ConfigurationSettings.GetConfig("MySettings") as MySettings;
Response.Write(myset.SomeSetting);
}
得到的结果将会是在客户端显示This is a customer configuration setting。其实还有另一种更简单的方法,就是利用NameValueFileSectionHandler,但是在添加配置信息时需要像在AppSettings中那样用<add name="" value=""></add>来添加键值,对于自定义配置来说意义不大,具体可以参考msdn中相关的文章。
原文: http://www.cnblogs.com/roger/archive/2004/11/02/59788.aspx
string sqlStr = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
对于结构比较复杂的自定义配置,可以通过实现IConfigurationSectionHandler接口来实现这种机制。首先,创建MySettings类,该类仅包含了我需要的一些自定义配置的定义:
using System;
namespace myconfig
using System;
using System.Configuration;
using System.Xml;
namespace myconfig
<configSections>
<section name="MySettings" type="myconfig.MyConfigHandler,myconfig"></section>
</configSections>
<MySettings>
<SomeSetting>This is a customer configuration setting.</SomeSetting>
</MySettings>
其中<configSecions>告诉web.config调用MyConfigHandler来处理MySettings,<MySettings>中保存的就是自定义的配置内容,例如在某个web page中执行如下代码:
private void Page_Load(object sender, System.EventArgs e)
// Put user code to initialize the page here
MySettings myset;
myset = System.Configuration.ConfigurationSettings.GetConfig("MySettings") as MySettings;
Response.Write(myset.SomeSetting);
}
得到的结果将会是在客户端显示This is a customer configuration setting。其实还有另一种更简单的方法,就是利用NameValueFileSectionHandler,但是在添加配置信息时需要像在AppSettings中那样用<add name="" value=""></add>来添加键值,对于自定义配置来说意义不大,具体可以参考msdn中相关的文章。
原文: http://www.cnblogs.com/roger/archive/2004/11/02/59788.aspx
相关文章推荐
- 为asp.net程序添加自定义配置区域
- 为asp.net程序添加自定义配置区域
- HOW TO:在 Visual C# .NET 中创建自定义 ASP.NET 配置节处理程序
- ASP.NET 用户配置 Part.3(自定义用户配置提供程序)
- ASP.NET之自定义异步HTTP处理程序(图文教程)
- C# ASP.NET 通用权限管理系统组件源码中WCF例子程序客户端运行详细配置参考
- ASP.NET 自定义成员资格提供程序 Part.3(实现提供程序类:XmlRoleProvider)
- ASP.NET : 如何为网站添加自定义的文件类型
- 在ASP.NET项目中的web.config文件里配置数据库连接并在程序代码中获取连接字符串
- 关于asp.net中gridview的问题,关于footer,16aspx上下的英语交流网程序,管理员的添加和修改有问题
- ckeditor和ckfinder在asp.net中的配置(分页,自定义按钮)
- Asp.Net Forms验证(自定义、角色提供程序)
- ASP.NET Forms验证(自定义、角色提供程序)
- 配置Asp.net程序错误集锦
- Asp.Net Forms验证(自定义、角色提供程序、单点登录) [转]
- ASP.NET 控制台程序 Windows开机启动项添加或移除如何设置
- ASP.net手动配置成员资格Membership提供程序
- C# ASP.NET 通用权限管理系统组件源码中WCF例子程序服务器端运行详细配置参考
- asp.net利用自定义Http处理程序实现URL伪静态之初探
- 以编程方式向 ASP.NET 控件添加客户端事件处理程序--TextBox.Attributes.Add()