千家信息网

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

发表于:2025-02-03 作者:千家信息网编辑
千家信息网最后更新 2025年02月03日,这篇文章主要介绍在web.config或者app.config中如何增加自定义配置节,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!简单键值对web.config
千家信息网最后更新 2025年02月03日在web.config或者app.config中如何增加自定义配置节

这篇文章主要介绍在web.config或者app.config中如何增加自定义配置节,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

简单键值对

web.config

                                                                                                                               

users.config

    

c#

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

复杂类型

web.config

                                                                         

roles.config

    

RolesCofig.cs

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#

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

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

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();            for(int i=0;i
var roles = System.Configuration.ConfigurationManager.GetSection("roles") as List;          Response.Write(roles.First ().Username);

以上是"在web.config或者app.config中如何增加自定义配置节"这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注行业资讯频道!

0