参考此文:https://www.cnblogs.com/chuankang/p/8776124.html
里面还有更多的用法
简单的代码如下,声明这个类
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;
public class ConfigService { public static IConfiguration Configuration { get; set; } static ConfigService() { //ReloadOnChange = true 当appsettings.json被修改时重新加载 Configuration = new ConfigurationBuilder() .Add(new JsonConfigurationSource { Path = "appsettings.json", ReloadOnChange = true }) .Build(); } }
使用的时候:
ConnectionString = ConfigService.Configuration["ConnectionStrings:Sqlserver"],