ASP.NET使用ConfigurationSection在Web.Config创建自定义配置节集合

核心代码

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
{
    public class DefaultElement : ConfigurationElement
    {
        [ConfigurationProperty("factory")]
        public string Factory
        {
            get
            {
                return this["factory"] as string;
            }
            set
            {
                this["factory"] = value;
            }
        }
    } 

    public class FactoryElement : ConfigurationElement
    {
        [ConfigurationProperty("name")]
        public string Name
        {
            get
            {
                return this["name"] as string;
            }
            set
            {
                this["name"] = value;
            }
        }

        [ConfigurationProperty("assembly")]
        public string Assembly
        {
            get
            {
                return this["assembly"] as string;
            }
            set
            {
                this["assembly"] = value;
            }
        }

        [ConfigurationProperty("class")]
        public string Class
        {
            get
            {
                return this["class"] as string;
            }
            set
            {
                this["class"] = value;
            }
        }
    }

    public class FactoryElements : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new FactoryElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((FactoryElement)element).Name;
        }

        public FactoryElement this[string name]
        {
            get
            {
                return BaseGet(name) as FactoryElement;
            }
        }
    }

    public class DbFactorySection : ConfigurationSection
    {
        [ConfigurationProperty("default")]
        public DefaultElement DefaultFactory
        {
            get
            {
                return this["default"] as DefaultElement;
            }
            set
            {
                this["default"] = value;
            }
        }
        [ConfigurationProperty("factorys")]
        public FactoryElements Factorys
        {
            get
            {
                return this["factorys"] as FactoryElements;
            }
            set
            {
                this["factorys"] = value;
            }
        }
    }

}

配置文件

  <configSections>
    <section name="dbFactory" type="Commons.DbFactorySection"/>
  </configSections>

  <dbFactory >
    <default factory="sql"></default>
    <factorys>
      <add name="sql"  assembly="Hello.Data"  class="SqlDbFactory" />
      <add name="oracle" assembly="Hello.Data" class="OracleDbFactory" />
      <add name="sqlite" assembly="Hello.sqlite" class="SqliteDbFactory" />
      <!--还可以添加无数个<add 节点-->
    </factorys>
  </dbFactory>

代码中使用

DbFactorySection dfs = ConfigurationManager.GetSection("dbFactory") as DbFactorySection;
FactoryElements fes = dfs.Factorys;
FactoryElement feSql = fes["sql"];
FactoryElement feSqlite = fes["sqlite"]
时间: 2024-07-29 05:01:47

ASP.NET使用ConfigurationSection在Web.Config创建自定义配置节集合的相关文章

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.WebC

在ASP.NET项目中的web.config文件里配置数据库连接并在程序代码中获取连接字符串

  1.在<connectionStrings> 标签里添加连接 <connectionStrings> <add name="ConnectionName" connectionString="Server=.\SQLEXPRESS;Database=DatabaseName;UserID=sa;Password=abc123" providerName="System.Data.SqlClient" />

在ASP.NET中创建自定义配置节(翻译)

2017年,共享经济持续成为大众关注的焦点,从共享单车.共享雨伞.共享充电宝,到共享电动车.共享汽车.共享床位,甚至连女友都拿来共享了.戴上"共享"高帽的创业项目一茬接一茬地冒出来,正如收割的韭菜,最开始两茬是最嫩的,接下来生长出来的则会让人觉得食之无味又弃之可惜.对于投资人如此,对于用户们来说有何尝不是呢? 让我们盘点下近一年出现过的"共享"明星们,对于它们,死亡还是生存?这是个问题. 据统计,2016年中国的共享经济市场规模接近4万亿元:2017年,共享系宣告进

ASP.NET添加和读取Web.Config自定义配置节

自定义节 1.首先在<configSections>中定义自定义配置节(例如Index.testSection)和对应的自定义配置节处理程序(例如NameValueSectionHandler) 2.然后添加节的内容 <configuration> <configSections> <sectionGroup name="Rewrite.NET"> <section name="Index" type="

asp.net项目中通过Web.config配置文件及文件夹的访问权限!

描述:在开发中我们通常会碰到这样的问题,例如:在项目的根目录下面有一个文件或者文件夹需要用户登陆后才能访问.如果用户在没有登录的情况下访问该文件或者该文件夹下面的文件时,直接拦截重定向到对应的登陆页面. 例一: 我想让用户在访问我的程序的Admin文件夹下的页面时需要登录,而在访问其他页面时则不需要,也就是说Admin文件夹下的文件拒绝匿名访问. 下面是配置根目录下的web.config文件中关于授权验证的配置. [xhtml] view plaincopy <system.web> <

Unity中Web.Config文件的配置与调用

在上一篇文章“Unit简单依赖注入”我们可以实现构造对象和被依赖对象之间的 松耦合,使我们的抽象层(Player)能够保持稳定,但是在并没有把客户类和Player类之间彻底解耦,即当我们不想使用MP3Player注入,而 想使用CDPlayer注入时,我们需要修改客户类的容器注册.下面我们使用web.config配置文件来解决这个问题.Unity 应用程序块可以从 XML 配置文件中读取配置信息.配置文件可以是 Windows Forms 应用程序的 App.config 或者 ASP.NET

修改和获取web.config或app.config文件appSettings配置节中的Add里的value属性 函数

1: /// <summary> 2: /// 修改web.config或app.config文件appSettings配置节中的Add里的value属性 3: /// </summary> 4: /// <remarks> 5: /// 注意,调用该函数后,会使整个Web Application重启,导致当前所有的会话丢失 6: /// </remarks> 7: /// <param name="key">要修改的键key

利用ASP.NET加密和解密Web.config中连接字符串

介绍 这篇文章我将介绍如何利用ASP.NET来加密和解密Web.config中连接字符串 背景描述 在以前的博客中,我写了许多关于介绍 Asp.net, Gridview, SQL Server, Ajax, JavaScript等的文章.大多数情况下,我都把数据库的连接字符串放在了web.config中.其中包含许多敏感信息,包括连接数据库的用户名密码等.然而我们在web.config和machine.config中以纯文本的方式保存密码安全吗? 如果我们的程序只是部署在内部服务器中,这应该没

ASP.NET5实践01:Web项目创建、结构概述、程序运行、发布部署

1.项目创建 ASP.NET5项目模板有三种: 新建项目: 选择模板: 2.结构概述 References对应配置是project.json中: "frameworks": { "dnx451": { }, "dnxcore50": { } }, ASP.NET5开发时支持多版本的clr共存,但运行时是使用其中一种. dnxcore50是跨平台.模块化的coreclr.它有多种,如:dnx-coreclr-win-*,dnx-coreclr-li