77 lines
1.9 KiB
C#
77 lines
1.9 KiB
C#
#region
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.Configuration.Json;
|
|
|
|
#endregion
|
|
|
|
namespace Web.Core.Common.Helper;
|
|
|
|
/// <summary>
|
|
/// appsettings.json操作类
|
|
/// </summary>
|
|
public class AppSettings
|
|
{
|
|
private static IConfiguration _configuration { get; set; }
|
|
private static string contentPath { get; set; }
|
|
|
|
public AppSettings(string contentPath)
|
|
{
|
|
var Path = "appsettings.json";
|
|
_configuration = new ConfigurationBuilder()
|
|
.SetBasePath(contentPath)
|
|
.Add(new JsonConfigurationSource
|
|
{
|
|
Path = Path,
|
|
Optional = false,
|
|
ReloadOnChange = true
|
|
}).Build();
|
|
}
|
|
|
|
public AppSettings(IConfiguration configuration)
|
|
{
|
|
_configuration = configuration;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取连接字符串
|
|
/// </summary>
|
|
/// <param name="key"></param>
|
|
/// <returns></returns>
|
|
public static string GetConnectionString(string key)
|
|
{
|
|
return _configuration.GetConnectionString(key) ?? "";
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取层级信息
|
|
/// </summary>
|
|
/// <param name="str"></param>
|
|
/// <returns></returns>
|
|
public static string GetLogLevel(string str)
|
|
{
|
|
return _configuration[str] ?? "";
|
|
}
|
|
|
|
/// <summary>
|
|
/// 强类型转换,可配置默认值
|
|
/// </summary>
|
|
/// <param name="str"></param>
|
|
/// <param name="defaultvalue">默认值</param>
|
|
/// <returns></returns>
|
|
public static int GetValue(string str, int defaultvalue)
|
|
{
|
|
return _configuration.GetValue(str, defaultvalue);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取信息数组
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="str"></param>
|
|
/// <returns></returns>
|
|
public static List<T> GetList<T>(string str)
|
|
{
|
|
return _configuration.GetSection(str).Get<List<T>>();
|
|
}
|
|
} |