68 lines
2.1 KiB
C#
68 lines
2.1 KiB
C#
using Microsoft.Extensions.Configuration.Json;
|
|
|
|
namespace Cms.Core.WebApi
|
|
{
|
|
public class InitConfiguration
|
|
{
|
|
|
|
static IConfiguration Configuration { get; set; }
|
|
|
|
|
|
|
|
static InitConfiguration()
|
|
{
|
|
var appsettings = "appsettings.json";
|
|
|
|
if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") != null
|
|
&& Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Production")
|
|
{
|
|
var envAppsettings = $"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json";
|
|
Configuration = new ConfigurationBuilder()
|
|
.Add(new JsonConfigurationSource { Path = appsettings, Optional = false, ReloadOnChange = true })
|
|
.Add(new JsonConfigurationSource { Path = envAppsettings, Optional = false, ReloadOnChange = true })
|
|
.Build();
|
|
return;
|
|
}
|
|
Configuration = new ConfigurationBuilder()
|
|
.Add(new JsonConfigurationSource { Path = appsettings, Optional = false, ReloadOnChange = true })
|
|
.Build();
|
|
}
|
|
public InitConfiguration(IConfiguration configuration)
|
|
{
|
|
Configuration = configuration;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 封装要操作的字符
|
|
/// </summary>
|
|
/// <param name="sections">节点配置</param>
|
|
/// <returns></returns>
|
|
public static string app(params string[] sections)
|
|
{
|
|
try
|
|
{
|
|
|
|
if (sections.Any())
|
|
{
|
|
return Configuration[string.Join(":", sections)];
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
|
|
}
|
|
|
|
return "";
|
|
}
|
|
|
|
public static string GetConnectionString(string key)
|
|
{
|
|
return Configuration.GetConnectionString(key);
|
|
}
|
|
public static IConfigurationSection GetSection(string key)
|
|
{
|
|
return Configuration.GetSection(key);
|
|
}
|
|
}
|
|
}
|