一种是网上常见的一种
var builder1 = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json"); var configuration = builder1.Build(); string baseUrl = configuration.GetSection("ConnectionStrings:xxx").Value;
另外一种是
var config = new ConfigurationBuilder() .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) //.AddEnvironmentVariables() .Build(); Configuration = config.GetSection("ConnectionStrings:xxx");