3

在「過去」使用XML配置是可能的包括像this從另一個文件部分配置:參考appsettings.json另一個JSON文件ASP.NET核心配置

<appSettings file="relative file name"> 
    <!-- Remaining configuration settings --> 
</appSettings> 

現在,在ASP。 NET Core我想分享一些配置appsettings-Development.json以及例如appsettings-Staging.json。是否也有類似<appSettings file="">使用json配置?

+5

,你在你的啓動代碼喜歡,你可以添加儘可能多的JSON文件。 – DavidG

+0

當然,但我試圖防止json文件本身之間的重複 –

+2

目前還不清楚「防止重複」是什麼意思。您不需要在其他文件中有重複的內容,只需根據需要添加新的位,並且框架將其合併爲更大的配置。如果每個附加文件都存在,則每個附加文件都會覆蓋上一個文件中的值,否則只會添加到其中。 – DavidG

回答

6

您可以在啓動類中添加複式配置文件:

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(); 
    } 

在這裏看到更多的細節:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration ...

+0

對不起,但那不是我的問題。例如,我需要將'appsettings-shared.json'包含到'appsettings-Development.json'和'appsettings-Staging.json'中。 –

+1

您可以將這些共享設置保存在單獨的文件中並將其加載到啓動中嗎?這樣,您不必將這些設置包含在其他設備中,因爲您始終可以使用這些設置。 – anserk

相關問題