从类库中读取不同环境的appSettings.json

问题描述 投票:0回答:0

我正在开发一个 .NET Core MVC Web API,其中有不同的类库。例如,我有类库 XYZ.DAL,我想根据不同的环境(开发、暂存或生产)读取其中的 appSettings.json。

我在 Web API(NET Core6) Program.cs 中编写了如下代码,其中 EnvName 是一个设置值 Development、Staging 或 Production

var EnvName = builder.Configuration.GetSection("EnvName").Value;
builder.Configuration
    .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
    .AddJsonFile($"appsettings.{EnvName}.json", optional: true);

如何读取类库中的appSettings?

asp.net-mvc asp.net-core webapi appsettings class-library
© www.soinside.com 2019 - 2024. All rights reserved.