话不多说直接上代码:
public static class MengTConfig { static IConfigurationRoot _MT_Configuration { get; } static IConfigurationSection _MT_AppSetting; static MengTConfig() { var builder = new ConfigurationBuilder() .SetBasePath(AppContext.BaseDirectory) .AddJsonFile("_config/MengT.json", optional: true, reloadOnChange: true); _MT_Configuration = builder.Build(); _MT_AppSetting = _MT_Configuration.GetSection("AppSettings"); } /// <summary> /// 通过[Key]获取数据库连接字符串 /// </summary> /// <param name="_key">Key</param> /// <returns></returns> public static string GetConnectionString(string _key) { return _MT_Configuration.GetConnectionString(_key); } /// <summary> /// 通过[Key]获取配置信息 /// </summary> /// <param name="_key">Key</param> /// <returns></returns> public static string GetAppSetting(string _key) { return _MT_AppSetting[_key]; } }