【优化】AppSettingsHelper 支持按环境加载配置文件,非生产环境下 appsettings.{Environment}.json 可覆盖主配置。

This commit is contained in:
大灰灰
2026-09-13 06:50:01 +08:00
parent 67011df925
commit fa7705f7b9
3 changed files with 20 additions and 5 deletions
+18 -3
View File
@@ -19,10 +19,25 @@ namespace CoreCms.Net.Configuration
{
static IConfiguration Configuration { get; set; }
public AppSettingsHelper(string contentPath)
public AppSettingsHelper(string contentPath, string environmentName)
{
string Path = "appsettings.json";
Configuration = new ConfigurationBuilder().SetBasePath(contentPath).Add(new JsonConfigurationSource { Path = Path, Optional = false, ReloadOnChange = true }).Build();
var configBuilder = new ConfigurationBuilder()
.SetBasePath(contentPath)
.Add(new JsonConfigurationSource { Path = "appsettings.json", Optional = false, ReloadOnChange = true });
//非 Production 环境追加加载对应的环境配置文件(如 appsettings.Development.json),用于本地覆盖默认配置
if (!string.IsNullOrEmpty(environmentName) &&
!environmentName.Equals("Production", StringComparison.OrdinalIgnoreCase))
{
configBuilder.Add(new JsonConfigurationSource
{
Path = $"appsettings.{environmentName}.json",
Optional = true,
ReloadOnChange = true
});
}
Configuration = configBuilder.Build();
}
/// <summary>