ASP.NET使用ConfigurationSection在Web.Config创建自定义配置节
主要代码,一定要继续System.Configuration.ConfigurationSection,具体的节点名称可以自行修改
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls;namespace Commons {/// <summary>/// ConfigSection 的摘要说明/// </summary>public class MyConfigSection : ConfigurationSection{public MyConfigSection(){ //// TODO: 在此处添加构造函数逻辑// }[ConfigurationProperty("user", DefaultValue = "admin", IsRequired = true)]public string User{get { return (string)this["user"]; }set { this["user"] = value; }}[ConfigurationProperty("password", DefaultValue = "password", IsRequired = true)]public string PassWord{get { return (string)this["password"]; }set { this["password"] = value; }}[ConfigurationProperty("element")]public elementinfo Element{get { return (elementinfo)this["element"]; }set { this["element"] = value; }}}public class elementinfo : ConfigurationElement{public elementinfo() { }[ConfigurationProperty("element1", DefaultValue = "element1", IsRequired = true)]public string Element1{get { return (string)this["element1"]; }}[ConfigurationProperty("element2", DefaultValue = "element2", IsRequired = true)]public string Element2{get { return (string)this["element2"]; }}} }
View Code
配置文件
<configSections><sectionGroup name="mygroup"><section name="mysection" type="Commons.MyConfigSection" allowDefinition="Everywhere" allowLocation="true"/></sectionGroup></configSections> <mygroup><mysection user="用户" password="密码"><element element1="属性1" element2="属性2"></element></mysection></mygroup>
代码中使用值
Commons.MyConfigSection config = (Commons.MyConfigSection)ConfigurationManager.GetSection("mygroup/mysection");Response.Write("用户名:" + config.User.ToString() + "密码:" + config.PassWord.ToString() + "元素属性:" + config.Element.Element1.ToString() + config.Element.Element2.ToString());
转载于:https://www.cnblogs.com/yonsy/p/5620563.html
ASP.NET使用ConfigurationSection在Web.Config创建自定义配置节相关推荐
- 如何在.config文件自定义配置节
在.NET程序中一般都有个应用程序配置文件在ASP.NET中是web.config,在控制台应用程序和Windows应用程序中是app.config.我们通常使用此配置文件来设置一些程序会用到的信息, ...
- Web.config中创建自定义配置节
Web.config文件可谓是每个ASP.NET站点必不可少的一个文件,其中记录了网站的各种配置.一些比较静态但是又可能变更的数据可以放在web.config中,已有的appSettings配置节便有 ...
- PetShop 4.0知识点:加密和解密Web.config文件的配置节
PetShop 4.0 中Web.config文件中的连接字符串是加密的,需要进行解密才能看到. 这个问题PetShop 4.0的ReadMe文件中有说明: Encrypting and Decryp ...
- ASP.NET2.0里的web.config配置接口API(转)
ASP.NET2.0里的配置接口API ASP.NET2.0里的配置API接口函数允许我们读/写配置文件----包括对web.config和machine.config的读写.您可以读写您自己应用程序 ...
- 在web.config中添加配置信息
1 在web.config中添加配置信息<configuration><connectionStrings><add name="conStr" co ...
- Unity中Web.Config文件的配置与调用
在上一篇文章"Unit简单依赖注入"我们可以实现构造对象和被依赖对象之间的 松耦合,使我们的抽象层(Player)能够保持稳定,但是在并没有把客户类和Player类之间彻底解耦,即 ...
- ASP.NET Core 配置 - 创建自定义配置提供程序
ASP.NET Core 配置 - 创建自定义配置提供程序 在本文中,我们将创建一个自定义配置提供程序,从数据库读取我们的配置.我们已经了解了默认配置提供程序的工作方式,现在我们将实现我们自己的自定义 ...
- 开发手记之实现web.config的快速配置(转载)
开发手记之实现web.config的快速配置 开发手记之实现web.config的快速配置 作者:Anytao 问题简述: 在Web开发中,对web.cofig进行配置是非技术人员无法胜任的工作,但是 ...
- C# App.config 自定义 配置节
方法一. App.config <?xml version="1.0" encoding="utf-8" ?> <configuration& ...
最新文章
- Pandas的DataFrame数据类型
- Unity 2D游戏开发教程之使用脚本实现游戏逻辑
- drcom linux怎么运行,drcom for linux
- lua 元表/元方法
- 创建可扩展性系统-4
- 史上最简洁易懂的PGP邮件加密教程(MAC OS X版)
- x86 Assembly Guide
- [转载] python中@property装饰器
- 安装MySql报错(This application requires .NET Framework x.x.x)
- 【opencv+C++】在图像中找四边形
- c oracle案例,Oracle 19c CRS重启案例记录
- MTK功能机RF射频参数计算与配置
- 15 年工龄的阿里P9职场历程自述
- java ssh ssm_JAVA --- SSH和SSM的区别
- (Word论文插入高清图片)软件设置教程
- sublime快捷键操作
- Shiro原理以及运行流程
- lighttpd http响应报文(Response)增加安全头Referrer-Policy和X-Permitted-Cross-Domain-Policies方法
- 大都会人寿三周的工作回顾
- 快速傅里叶算法(灌水)