欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Aso.Net Core 的配置系统Configuration

程序员文章站 2023-12-25 23:44:51
[TOC] Aso.Net Core 的配置系统Configuration ​ 1.以前的配置文件格式为XML ​ 2.新版的配置文件格式支持 { json ,xml, ini, memory, command, env..... } 01.Json文件的弱类型方式读取 1. Json文件 【Mic ......

aso.net core 的配置系统configuration

​ 1.以前的配置文件格式为xml

​ 2.新版的配置文件格式支持 { json ,xml, ini, memory, command, env..... }

01.json文件的弱类型方式读取

  1. json文件 【microsoft.extensions.configuration.json】

  2. 添加配置文件json文件,并设置内容

    {
      "database": {
        "sqlserver": {
          "connectionstring": "server=.;database=testdb;uid=sa;pwd=123;"
        },
        "mysql": {
          "ipaddress": "127.0.0.1",
          "port": 3306
        }
      },
      "endarray": [
        { "endid": 20 },
        { "endid": "30" }
      ]
    }
  3. 获取数据

    private static void main(string[] args)
    {
        iconfiguration configuration = new configurationbuilder()
            .setbasepath(environment.currentdirectory)
            .addjsonfile("appsettings.json",true,true)
            .addinmemorycollection()
            .build();
        var str1 = configuration["database:sqlserver:connectionstring"];
        var str2 = configuration["endarray:0:endid"];
        console.writeline($"{str1}+---{str2}");
    }

02.json文件的强类型获取方式

1.添加引用【microsoft.extensions.configuration.binder】

2.getvalue方式获取

private static void main(string[] args)
{
    iconfiguration configuration = new configurationbuilder()
        .setbasepath(environment.currentdirectory)
        .addjsonfile("appsettings.json",true,true)
        .addinmemorycollection()
        .build();

    //getvalue获取方式
    int val = configuration.getvalue<int>("endarray:0:endid");
    console.writeline(val); 
}

3.实体映射方式

private static void main(string[] args)
{
    iconfiguration configuration = new configurationbuilder()
        .setbasepath(environment.currentdirectory)
        .addjsonfile("appsettings.json",true,true)
        .addinmemorycollection()
        .build();
 
    //bind 获取方式
    root root=new root();
    configuration.bind(root);
    var ip = root.database.mysql.ipaddress;
    var entid = root.endarray[0].endid;
    console.writeline($"{ip}{entid}");
    //get<t> 获取
    var root = configuration.get<root>();
    var ip = root.database.mysql.ipaddress;
    var entid = root.endarray[0].endid;
    console.writeline($"{ip}{entid}");
 
}
//这里是实体类  
 public class sqlserver
    {
        /// <summary>
        /// 
        /// </summary>
        public string connectionstring { get; set; }
    }

    public class mysql
    {
        /// <summary>
        /// 
        /// </summary>
        public string ipaddress { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public int port { get; set; }
    }

    public class database
    {
        /// <summary>
        /// 
        /// </summary>
        public sqlserver sqlserver { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public mysql mysql { get; set; }
    }

    public class endarray
    {
        /// <summary>
        /// 
        /// </summary>
        public int endid { get; set; }
    }

    public class root
    {
        /// <summary>
        /// 
        /// </summary>
        public database database { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public list<endarray> endarray { get; set; }
    }

上一篇:

下一篇: