core中静态类如何读取json配置
最近工作中遇到了这个问题,解决如下
类增加属性
public static string HostName { get; set; } public static string UserName { get; set; } public static string Password { get; set; } public static int Port { get; set; }
在startup.cs里增加方法
private void SetSetting() { RmHelper.HostName = Configuration.GetValue<string>("HostName"); RmHelper.Port= Configuration.GetValue<int>("Port"); RmHelper.UserName = Configuration.GetValue<string>("UserName"); RmHelper.Password = Configuration.GetValue<string>("Password"); }
RmHelper是我的类。
public Startup(IConfiguration configuration) { Configuration = configuration; SetSetting();(); //注意这里 }