• net 中web.config一个配置文件解决方法 (其他配置文件引入方式)


       近期一个项目需要写许多的配置项,发现在单个web.config里面写的话会很乱也难于查找

    所以搜了一下解决了,记录下来

     

     

    一、   webconfig提供了引入其他config的方式

    <connectionStrings configSource="Configsdatabase.config" />

    这个是连接字符串的配置你可以在database。config里面写很多链接字符串以备自己调用

    database。config里面的内容如下:

    <?xml version="1.0" encoding="utf-8"?>
    <connectionStrings>
      <add name="SDbContext" connectionString="Server=.;Initial Catalog=Self;User ID=sa;Password=password" providerName="System.Data.SqlClient" />
     
    </connectionStrings>


    <appSettings configSource="Configssystem.config" />

    这个是键值对的方式存放代码如下:

    <?xml version="1.0" encoding="utf-8"?>
    <appSettings>
      <!-- ================== 1:开发系统相关配置 ================== -->
      <!-- 登陆提供者模式:Session、Cookie-->
      <add key="LoginProvider" value="Cookie" />
      <!-- 启用系统日志-->
      <add key="IsLog" value="true" />
      <!-- 数据库超时间-->
      <add key="CommandTimeout" value="180" />
      <!--启用IP过滤 -->
      <add key="IsIPFilter" value="false" />
      <!-- ================== 2:系统软件参数配置 ================== -->
      <!-- 联系我们 -->
      <add key="Contact" value="TE Software(Mobility)" />
      <!-- 软件名称 -->
      <add key="SoftName" value="Sub Self" />
      <!-- 软件版本 -->
      <add key="Version" value="1.0" />
    
      <!-- 设置就应用路径 -->
      <add key="AppName" value="" />
    
      <!-- 设置就应用路径 -->
      <add key="SqlGetBomList" value="" />
    </appSettings>

    以上两个是不需要特殊的配置的,放到configuration里面 configSections的下面这样就可以

    二、下面介绍自定义配置节

    <configSections>
                                                                                                                                 
       <section name="users" type="System.Configuration.NameValueSectionHandler"/>
                                                                                                                                  
                                                                                                                                  
     </configSections>
     <users  configSource="users.config"></users>

    注意configsections里面的一条,是声明这是以什么组织方式

    users.config 里面的内容如下:

    <users>
      <add key="beijing" value="123"></add>
      <add key="tianjin" value="123"></add>
    </users>

    获取配置的方式:

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

      三、复杂类型:

    复杂类型的声明就不同了

    <configSections>
                                                                              
    <section name="roles" type="EBuy.Chapter3.NTier.WebUI.RolesConfig, EBuy.Chapter3.NTier.WebUI"/>
                                                                              
    </configSections>
    <roles configSource="roles.config">
      </roles>

    内容如下

    <roles>
      <add username="beijing" password="123"></add>
      <add username="tianjin" password="123"></add>
    </roles>

    获取方式:

    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;
            }
        }
    }
      
    
    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<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;}
        }
    }
      
    
    var roles = System.Configuration.ConfigurationManager.GetSection("roles") as List<EBuy.Chapter3.NTier.WebUI.Role >;
              Response.Write(roles.First ().Username);
    

      

  • 相关阅读:
    数据库镜像搭建
    关于开发人员数据库权限配置以及规范数据库升级流程
    带CheckBox列头的DataGridView
    查询整个数据库中某个特定值所在的表和字段的方法
    SQL Server 2008中获取数据库所有表及其字段名称、类型、长度的SQL
    关于已经上线项目的升级的启示
    SQL语句恢复数据库时一直显示“正在还原”
    带CheckBox列头的DataGridView(一)
    SQL Server中事务处理的注意事项
    group by 使用
  • 原文地址:https://www.cnblogs.com/barnet/p/7132522.html
Copyright © 2020-2023  润新知