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();(); //注意这里
        }

 

 

  没有英汉互译结果
  请尝试网页搜索

posted @ 2019-06-28 17:24  道#  阅读(432)  评论(0编辑  收藏  举报