为asp.net程序添加自定义配置区域
2004-11-02 21:41
996 查看
我们通常把诸如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中相关的文章。
Justin同学总是push我要多发些技术贴,否则就有点对不起他推荐我到这里来了,过两天有空的话就再写些关于自定义httpHandlers和httpModules的文章吧,hoho,被人push真不爽。
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中相关的文章。
Justin同学总是push我要多发些技术贴,否则就有点对不起他推荐我到这里来了,过两天有空的话就再写些关于自定义httpHandlers和httpModules的文章吧,hoho,被人push真不爽。
相关文章推荐
- 为asp.net程序添加自定义配置区域 (转)
- 为asp.net程序添加自定义配置区域
- HOW TO:在 Visual C# .NET 中创建自定义 ASP.NET 配置节处理程序
- ASP.NET 用户配置 Part.3(自定义用户配置提供程序)
- asp.net+access程序运行环境的配置(转载)
- ASP.NET - 配置文件提供程序
- 自定义ASP.NET Identity(三)- 实现自定义MySQL ASP.NET Identity存储提供程序
- ASP.NET 之 自定义 同步HTTP处理程序
- ASP.NET 之 自定义 异步HTTP处理程序
- Asp.Net Forms验证(自定义、角色提供程序、单点登录)
- NET 2.0中WinForm自定义的程序配置文件Settings.setting使用方法
- ASP.NET程序通过web.config为网站添加MIME
- 创建自定义路由处理程序(Creating a Custom Route Handler) | 定制路由系统| 高级路由特性 |精通ASP-NET-MVC-5-弗瑞曼
- asp.net中一般处理程序中添加session
- ASP.NET中把扩展名.aspx改成自定义的扩展名.mspx,如何在IIS与.net框架中配置
- Asp.Net Forms验证(自定义、角色提供程序、单点登录)
- IIS7.5配置IIS脚本映射实现Asp.Net的Http管道程序
- ASP.NET 实践:使用 ConfigurationSection 创建自定义配置段
- NET 2.0中WinForm自定义的程序配置存放到哪里去了
- asp.net中,如果使用自定义的 IHttpHandler时配置说明