在web.config或者app.config中增加自定义配置节

简介:

简单键值对


web.config


1
2
3
4
5
6
7
< configSections >
                                                                                                                             
    < section  name = "users"  type = "System.Configuration.NameValueSectionHandler" />
                                                                                                                              
                                                                                                                              
  </ configSections >
  < users   configSource = "users.config" ></ users >

users.config

1
2
3
4
< users >
   < add  key = "beijing"  value = "123" ></ add >
   < add  key = "tianjin"  value = "123" ></ add >
</ users >


c#

1
2
NameValueCollection users = System.Configuration.ConfigurationManager.GetSection( "users" as  NameValueCollection;
             Response.Write(users.Keys[0]+ "</br>" +users.Keys[1]);


复杂类型

web.config


1
2
3
4
5
6
7
< configSections >
                                                                          
< section  name = "roles"  type = "EBuy.Chapter3.NTier.WebUI.RolesConfig, EBuy.Chapter3.NTier.WebUI" />
                                                                          
</ configSections >
< roles  configSource = "roles.config" >
   </ roles >

roles.config

1
2
3
4
< roles >
   < add  username = "beijing"  password = "123" ></ add >
   < add  username = "tianjin"  password = "123" ></ add >
</ roles >


RolesCofig.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Web;
namespace  EBuy.Chapter3.NTier.WebUI
{
     public  class  RolesConfig : System.Configuration.IConfigurationSectionHandler
     {
         public  object  Create( object  parent,  object  configContext, System.Xml.XmlNode section)
         {
             return  section;
         }
     }
}



c#

1
2
XmlNode roles= System.Configuration.ConfigurationManager.GetSection( "roles" as  XmlNode;
            Response.Write(roles.ChildNodes [0].Attributes[ "username" ].InnerText);

还可以将配置节定义为一个实体

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Web;
namespace  EBuy.Chapter3.NTier.WebUI
{
     public  class  RolesConfig : System.Configuration.IConfigurationSectionHandler
     {
         public  object  Create( object  parent,  object  configContext, System.Xml.XmlNode section)
         {
             var  list= new  List<Role>();
             for ( int  i=0;i<section.ChildNodes.Count;i++)
             {
                 list.Add( new  Role (){
                     Username =section.ChildNodes[i].Attributes[ "username" ].InnerText ,
                     Password =section.ChildNodes[i].Attributes[ "password" ].InnerText });
             }
             return  list;
         }
     }
     public  class  Role
     {
         public  string  Username {  get set ; }
         public  string  Password{ get ; set ;}
     }
}
1
2
var  roles = System.Configuration.ConfigurationManager.GetSection( "roles" as  List<EBuy.Chapter3.NTier.WebUI.Role >;
           Response.Write(roles.First ().Username);




本文转自 virusswb 51CTO博客,原文链接:http://blog.51cto.com/virusswb/1374335,如需转载请自行联系原作者
目录
相关文章
|
1月前
create-react-app配置环境变量
create-react-app配置环境变量
73 0
|
1月前
|
Java 数据库连接 开发工具
web后端-SpringCloud-Config分布配置
web后端-SpringCloud-Config分布配置
|
1月前
|
小程序 开发工具 git
【微信小程序】-- uni-app 项目--- 购物车 -- 配置 tabBar 效果(五十一)
【微信小程序】-- uni-app 项目--- 购物车 -- 配置 tabBar 效果(五十一)
|
1月前
|
开发框架 移动开发 小程序
【微信小程序】-- 配置uni-app的开发环境(四十八)
【微信小程序】-- 配置uni-app的开发环境(四十八)
|
10天前
|
JavaScript
vue.config.ts配置环境变量
vue.config.ts配置环境变量
16 0
|
1月前
|
移动开发 监控 小程序
mPaaS常见问题之uniapp ios端云打包的配置config文件如何解决
mPaaS(移动平台即服务,Mobile Platform as a Service)是阿里巴巴集团提供的一套移动开发解决方案,它包含了一系列移动开发、测试、监控和运营的工具和服务。以下是mPaaS常见问题的汇总,旨在帮助开发者和企业用户解决在使用mPaaS产品过程中遇到的各种挑战
27 0
|
1月前
|
前端开发 Android开发 iOS开发
应用研发平台EMAS使用 aliyun-react-native-push 库接入推送和辅助通道,推送都可以收到,但是在App切到后台或者杀掉进程之后就收不到推送了,是需要配置什么吗?
【2月更文挑战第31天】应用研发平台EMAS使用 aliyun-react-native-push 库接入推送和辅助通道,推送都可以收到,但是在App切到后台或者杀掉进程之后就收不到推送了,是需要配置什么吗?
32 2
|
1月前
|
数据库
最全三大框架整合(使用映射)——struts.xml和web.xml配置
最全三大框架整合(使用映射)——数据库资源文件jdbc.properties
10 0
|
1月前
|
消息中间件 SpringCloudAlibaba Java
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(八)Config服务配置+bus消息总线+stream消息驱动+Sleuth链路追踪
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(八)Config服务配置+bus消息总线+stream消息驱动+Sleuth链路追踪
785 0
|
1月前
|
Java 关系型数据库 应用服务中间件
JAVA Web项目开发eclipse工具包配置(第一天)
JAVA Web项目开发eclipse工具包配置(第一天)

热门文章

最新文章