From fa7705f7b9fa9b0aaa856cb93c997afb1b23fe36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E7=81=B0=E7=81=B0?= Date: Sun, 13 Sep 2026 06:50:01 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E4=BC=98=E5=8C=96=E3=80=91AppSettings?= =?UTF-8?q?Helper=20=E6=94=AF=E6=8C=81=E6=8C=89=E7=8E=AF=E5=A2=83=E5=8A=A0?= =?UTF-8?q?=E8=BD=BD=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6=EF=BC=8C=E9=9D=9E?= =?UTF-8?q?=E7=94=9F=E4=BA=A7=E7=8E=AF=E5=A2=83=E4=B8=8B=20appsettings.{En?= =?UTF-8?q?vironment}.json=20=E5=8F=AF=E8=A6=86=E7=9B=96=E4=B8=BB=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AppSettingsHelper.cs | 21 ++++++++++++++++--- CoreCms.Net.Web.Admin/Program.cs | 2 +- CoreCms.Net.Web.WebApi/Program.cs | 2 +- 3 files changed, 20 insertions(+), 5 deletions(-) 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缓存