.Net Core 2.0 App中读取appsettings.json

引用:

Microsoft.Extensions.Configuration
Microsoft.Extensions.Configuration.FileExtensions
Microsoft.Extensions.Configuration.Json

创建json文件:

设置json文件

 

class Program
    {
        public static void Main(string[] args)
        {
            var builder = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);

            IConfigurationRoot configuration = builder.Build();

            Console.WriteLine(configuration.GetConnectionString("Tag1"));
            Console.WriteLine(configuration.GetSection("ConnectionStrings:Tag2").Value);
            Console.ReadKey();
        }
    }

 

posted @ 2019-08-21 17:10  jasonlai2016  阅读(202)  评论(0编辑  收藏  举报