vs2015+ 中C#项目读取JSON配置文件的方法
json配置文件
需要获取dbConn数据
public static string GetConnectionString(string value) { var configurationBuilder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.Development.json"); //var configurationBuilder = new ConfigurationBuilder() // .Add(new JsonConfigurationSource { Path = "config.json", ReloadOnChange = true }); IConfiguration config = configurationBuilder.Build(); string connectionString = config[value]; return connectionString; }
使用方法