net core 2 读取appsettings.json
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("Storage"));
Console.WriteLine(configuration.GetSection("ConnectionStrings:Storage").Value);
}
自己webapi程序不从Startup.cs读取,验证后修改如下
protected static IConfiguration Configuration { get; set; }
protected static string connection { get; set; }
static DataManager()
{
string ss = AppContext.BaseDirectory;
string aa = Directory.GetCurrentDirectory();
var builder = new ConfigurationBuilder()
.SetBasePath(AppContext.BaseDirectory)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
Configuration = builder.Build();
connection = Configuration.GetConnectionString("DefaultConnection");
}