net core控制台读取json配置

 

 

public class ConfigurationHelper
    {
        private static IConfigurationRoot ConfigurationRoot
        {
            get
            {
                //读取环境变量
                string env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");

                var build = new ConfigurationBuilder();
                build.SetBasePath(Directory.GetCurrentDirectory());
                //根据环境变量加载json配置
                build.AddJsonFile($"appsettings.{env}.json", false, false);
                var configuration = build.Build();

                return configuration;
            }
        }

        public static string GetValue(string key)
        {
            return ConfigurationRoot.GetSection(key).Value;
        }
    }

  

posted @ 2020-04-13 10:47  在路上的白羊  阅读(459)  评论(0编辑  收藏  举报