diff --git a/CoreCms.Net.Configuration/AppSettingsHelper.cs b/CoreCms.Net.Configuration/AppSettingsHelper.cs index 4916d5c..9c893aa 100644 --- a/CoreCms.Net.Configuration/AppSettingsHelper.cs +++ b/CoreCms.Net.Configuration/AppSettingsHelper.cs @@ -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(); } /// diff --git a/CoreCms.Net.Web.Admin/Program.cs b/CoreCms.Net.Web.Admin/Program.cs index 1a558b9..dfbecfd 100644 --- a/CoreCms.Net.Web.Admin/Program.cs +++ b/CoreCms.Net.Web.Admin/Program.cs @@ -33,7 +33,7 @@ using Yitter.IdGenerator; var builder = WebApplication.CreateBuilder(args); //添加本地路径获取支持 -builder.Services.AddSingleton(new AppSettingsHelper(builder.Environment.ContentRootPath)); +builder.Services.AddSingleton(new AppSettingsHelper(builder.Environment.ContentRootPath, builder.Environment.EnvironmentName)); builder.Services.AddSingleton(new LogLockHelper(builder.Environment.ContentRootPath)); //Memory缓存 diff --git a/CoreCms.Net.Web.WebApi/Program.cs b/CoreCms.Net.Web.WebApi/Program.cs index a8a97a6..45e068b 100644 --- a/CoreCms.Net.Web.WebApi/Program.cs +++ b/CoreCms.Net.Web.WebApi/Program.cs @@ -36,7 +36,7 @@ using NLog.Web; var builder = WebApplication.CreateBuilder(args); //添加本地路径获取支持 -builder.Services.AddSingleton(new AppSettingsHelper(builder.Environment.ContentRootPath)); +builder.Services.AddSingleton(new AppSettingsHelper(builder.Environment.ContentRootPath, builder.Environment.EnvironmentName)); builder.Services.AddSingleton(new LogLockHelper(builder.Environment.ContentRootPath)); //Memory缓存