You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET Core 6开发环境配置读取优先级及代码逻辑问询

.NET Core 6 配置加载与代码疑问解答

1. 开发环境下是否默认优先读取appsettings.Development.json?

是的,.NET Core 6 默认的配置加载机制中,**开发环境(Development)**下配置文件的优先级为:

  • appsettings.Development.json > appsettings.json

系统会先加载appsettings.json,再加载appsettings.Development.json,后者中的同名配置项会覆盖前者的内容。如果某个配置项仅存在于其中一个文件,则直接读取该文件中的对应值。

2. 给定代码片段在条件为真时的连接字符串来源?

先看代码片段:

builder.Services.AddDbContext<SteelworksContext>(options =>
{
   if (builder.Environment.IsDevelopment())
   {
       options.UseSqlServer(builder.Configuration.GetConnectionString("SteelworksDev"));   //from appsettings.Development
   }
   else
   {
       options.UseSqlServer(builder.Configuration.GetConnectionString("Steelworks"));  //from appsettings
   }
});

当builder.Environment.IsDevelopment()为真时:

  • GetConnectionString("SteelworksDev")会从合并后的全局配置集合中查找键为SteelworksDev的连接字符串。
  • 实际开发中,我们通常会将开发环境专属的SteelworksDev连接字符串单独定义在appsettings.Development.json中(appsettings.json一般不会包含这个键),因此此时读取的就是appsettings.Development.json中的值。
  • 若两个配置文件中都存在SteelworksDev键,开发环境下会优先使用appsettings.Development.json中的值。

内容的提问来源于stack exchange,提问作者Stefano Buzzoni

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 21:45:34