在appsettings.json中引用另一个用于ASP.NET Core配置的json文件

Kap*_*apé 8 c# configuration-files .net-core asp.net-core asp.net-core-2.0

在使用XML配置的"过去"中,可以包含来自另一个文件的部分配置,如下所示:

<appSettings file="relative file name">
    <!-- Remaining configuration settings -->
</appSettings>
Run Code Online (Sandbox Code Playgroud)

现在在ASP.NET Core中我想分享一些配置appsettings.Development.json以及例如appsettings.Staging.json.类似于<appSettings file="">使用json配置也可以使用吗?

ans*_*erk 12

您可以在Startup类中添加mutliple配置文件:

public class Startup
{
    public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .AddJsonFile("appsettings-Development.json", optional: false)
            .AddJsonFile("appsettings-Staging.json", optional: false)
            .AddEnvironmentVariables();
        Configuration = builder.Build();
    }
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅此处:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration ...

  • 您可以将这些共享设置保存在单独的文件中并在启动时加载它吗?这样,您不必将这些设置包括在其他设置中,因为您一直可以使用它们。 (3认同)
  • 对不起,但这不是我的问题。例如,我需要将`appsettings-shared.json`包含在`appsettings-Development.json`和`appsettings-Staging.json`中 (2认同)