依赖:
Microsoft.Extensions.Configuration
Microsoft.Extensions.Configuration.Binder
Microsoft.Extensions.Configuration.EnvironmentVariables
Microsoft.Extensions.Configuration.FileExtensions
Microsoft.Extensions.Configuration.Json
读取代码
var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) .AddEnvironmentVariables(); IConfigurationRoot configuration = builder.Build(); ConnectionStrings connectionStrings = new ConnectionStrings(); configuration.GetSection("ConnectionStrings").Bind(connectionStrings);
appsettings.json
{ "ConnectionStrings": { "SqlServer": "", "TiDB": "" } }