ASP NET自定义配置文件设置.docx
《ASP NET自定义配置文件设置.docx》由会员分享,可在线阅读,更多相关《ASP NET自定义配置文件设置.docx(6页珍藏版)》请在三一办公上搜索。
1、ASP NET自定义配置文件设置ASP.NET 自定义配置文件设置 在ASP.NET开发的过程中,我们经常会遇到在开发、测试、发布部署等不同的环境下使用不同的连接字符串,WebAPI的连接。为了避免频频复杂的修改各个配置,因此用到了自定义配置文件设置。 此篇文章使用的自定义配置文件方法为:web.config 增加自定义Section节,相关配置全部放到webconfig。 一、 配置Web.config文件。 1.0 首先需要在configuration节点下的configSections节点中进行注册,如下: 注:CustomConfig.Configrationner.Connectio
2、nStringSection指的是:此section 可解析的实体所存放的, CustomConfig指的是:此section所在的程序集的名称。 2.0 然后将ConnectionStringSection节点配置在configuration节点下即可,具体如下: 二、 配置调用ConfigrationSection的文件。 1.0 首先要访问这个自定义节点,需要通过类来配置,我们得首先定义一个父节点类,父节点类包含子节点集合,如下: namespace CustomConfig.Configrationner publicclassConnectionStringSection : Con
3、figurationSection ConfigurationProperty(Publish) publicConnectionStringCollection Publish get return (ConnectionStringCollection)thisPublish; ConfigurationProperty(Develop) publicConnectionStringCollection Develop get return (ConnectionStringCollection)thisDevelop; ConfigurationProperty(Test) public
4、ConnectionStringCollection Test get return (ConnectionStringCollection)thisTest; 2.0 子节点集合再包含子节点元素,如下 namespace CustomConfig.Configrationner publicclassConnectionStringCollection : ConfigurationElementCollection protectedoverrideConfigurationElement CreateNewElement returnnewNameValueSettings; prote
5、ctedoverrideobject GetElementKey(ConfigurationElement element) return (NameValueSettings)element).Name; /写一个索引器,方便的访问该集合中的元素。 /如果不写,则只有foreach来访问。 publicNameValueSettingsthisint index get returnthis.BaseGet(index) asNameValueSettings; publicnewNameValueSettingsthisstring key get returnthis.BaseGet(k
6、ey) asNameValueSettings; 3.0 定义子节点元素类,如下 namespace CustomConfig.Configrationner / /连接字符串类 / publicclassNameValueSettings : ConfigurationElement ConfigurationProperty(name, IsRequired = true) publicstring Name get returnthisname.ToString; set thisname = value; ConfigurationProperty(connectionString,
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- ASP NET自定义配置文件设置 NET 自定义 配置文件 设置

链接地址:https://www.31ppt.com/p-3153005.html