.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(); } }