.Net Core Global read configuration appsettins.json

Creating ConfigHelper.cs

1, ConfigHelper.cs code is as follows

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     }

 

Reference configuration ConfigHelper

1, the Startup.cs constructor, add the following

1  public Startup(IConfiguration configuration)
2         {
3             Configuration = configuration;
4             ConfigHelper._Init(configuration); //配置ConfigHelper
5         }

 

Test call

 

 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 just as static, easy to deposit. There are better ways to leave a message. .

Guess you like

Origin www.cnblogs.com/sevenmall/p/12018230.html