码迷,mamicode.com
首页 > Web开发 > 详细

Aso.Net Core 的配置系统Configuration

时间:2019-11-30 20:59:35      阅读:108      评论:0      收藏:0      [点我收藏+]

标签:directory   config   str1   引用   com   rect   console   配置系统   新版   

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; }
    }

Aso.Net Core 的配置系统Configuration

标签:directory   config   str1   引用   com   rect   console   配置系统   新版   

原文地址:https://www.cnblogs.com/Lueng/p/11963819.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!