(精华)2020年6月26日 C#类库 读取json配置文件帮助类

using Microsoft.Extensions.Configuration;
using System;

namespace Core.Util
{
     
    /// 
    /// 配置文件帮助类
    /// 
    public static class ConfigHelper
    {
     
        private static IConfiguration _config;
        private static object _lock = new object();
        public static IConfiguration Configuration
        {
     
            get
            {
     
                if (_config == null)
                {
     
                    lock (_lock)
                    {
     
                        if (_config == null)
                        {
     
                            var builder = new ConfigurationBuilder()
                                .SetBasePath(AppContext.BaseDirectory)
                                .AddJsonFile("appsettings.json");
                            _config = builder.Build();
                        }
                    }
                }

                return _config;
            }
            set
            {
     
                _config = value;
            }
        }

        /// 
        /// 从AppSettings获取key的值
        /// 
        /// key
        /// 
        public static string GetValue(string key)
        {
     
            return Configuration[key];
        }

        /// 
        /// 获取连接字符串
        /// 
        /// 连接字符串名
        /// 
        public static string GetConnectionString(string nameOfCon)
        {
     
            return Configuration.GetConnectionString(nameOfCon);
        }
    }
}

你可能感兴趣的:(#,C#类库/扩展方法,c#,asp.net,后端)