创建 ConfigHelper.cs
1,ConfigHelper.cs 代码如下
- public class ConfigHelper
- {
- private static IConfiguration Config = null; //ConfigurationRoot
- public static void _Init(IConfiguration Configuration)
- {
- Config = Configuration;
- }
- public static string Get(string Key)
- {
- IConfigurationSection ISection = Config.GetSection(Key);
- return ISection.Value;
- }
- public static void Set(string Key,string Value)
- {
- IConfigurationSection ISection = Config.GetSection(Key);
- ISection.Value = Value;
- }
- }
配置 ConfigHelper 引用
1, 在 Startup.cs 构造函数, 新增如下
- public Startup(IConfiguration configuration)
- {
- Configuration = configuration;
- ConfigHelper._Init(configuration); // 配置 ConfigHelper
- }
测试调用
- // appsettings.JSON 文件如下
- //{
- // "AppSettings": {
- // "downloadFileUrl": "https://hualai-glotx",
- // "localFileUrl": "D:\\mydata4vipday_en.datx",
- // "RefreshTokenValidDays": 30
- // }
- //}
- // 测试调用
- ConfigHelper.Get("AppSettings:localFileUrl");
ps: 如下只是将 Config 静态化, 方便取存. 有更好的方式可以留言..
来源: http://www.bubuko.com/infodetail-3325648.html