创建ConfigHelper.cs
1、ConfigHelper.cs 代码如下
1 public class ConfigHelper 2 { 3 private static IConfiguration Config = null; //ConfigurationRoot 4 public static void _Init(IConfiguration Configuration) 5 { 6 Config = Configuration; 7 } 8 9 public static string Get(string Key) 10 { 11 IConfigurationSection ISection = Config.GetSection(Key); 12 return ISection.Value; 13 } 14 15 public static void Set(string Key,string Value) 16 { 17 IConfigurationSection ISection = Config.GetSection(Key); 18 ISection.Value = Value; 19 } 20 }
配置ConfigHelper引用
1、在Startup.cs 构造函数,新增如下
1 public Startup(IConfiguration configuration) 2 { 3 Configuration = configuration; 4 ConfigHelper._Init(configuration); //配置ConfigHelper 5 }
测试调用
1 // appsettings.json 文件如下 2 //{ 3 // "AppSettings": { 4 // "downloadFileUrl": "https://hualai-glotx", 5 // "localFileUrl": "D:\\mydata4vipday_en.datx", 6 // "RefreshTokenValidDays": 30 7 // } 8 //} 9 10 //测试调用 11 ConfigHelper.Get("AppSettings:localFileUrl");
ps:如下只是将Config 静态化,方便取存。有更好的方式可以留言。。