简体   繁体   English

如何替换自定义 appsettings.json 文件中的列表项?

[英]How to replace list items in a custom appsettings.json file?

I have custom appsettings.json files for each environment, so appsettings.Dev.json, appsettings.Test.json, appsettings.Prod.json.我有针对每个环境的自定义 appsettings.json 文件,所以 appsettings.Dev.json、appsettings.Test.json、appsettings.Prod.json。 In the main appsettings.json, I have the following code:在主 appsettings.json 中,我有以下代码:

  "EmailSettings": {
    "Recipients": [
      "person@test.com"
    ]
  }

Then in the custom json file, I want to override this list, like this:然后在自定义 json 文件中,我想覆盖这个列表,如下所示:

  "EmailSettings": {
    "Recipients": [
      "anotherperson@test.com"
    ]
  }

But instead, this gets appended like this:但相反,它会像这样附加:

  "EmailSettings": {
    "Recipients": [
      "person@test.com",
      "anotherperson@test.com"
    ]
  }

With all other types of settings, they get replaced, but for some reason, it seems that lists in custom settings files get appended instead.对于所有其他类型的设置,它们会被替换,但出于某种原因,自定义设置文件中的列表似乎被附加了。 With .net, you used to have more granularity with the xslt to be able to determine whether you wanted to replace or append overridden settings.使用 .net,您曾经使用 xslt 有更多的粒度,以便能够确定是要替换还是附加覆盖的设置。 Any suggestions here?这里有什么建议吗?

SOLUTION (for me)解决方案(对我来说)

I did this, and it gets replaced in the custom json settings.我这样做了,它在自定义 json 设置中被替换了。 Main appsettings.json:主要的 appsettings.json:

  "EmailSettings": {
    "Recipients:0": "person@test.com"
  }

Then in the custom settings file:然后在自定义设置文件中:

  "EmailSettings": {
    "Recipients:0": "anotherperson@test.com"
  }

Thanks for the responses!感谢您的回复!

I utilized different appsettings files for environments (including Development )我为环境(包括Development )使用了不同的appsettings文件

So first make sure you have added the environment json file in your configuration:所以首先确保你已经在你的配置中添加了环境 json 文件:

config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{EnvironmentName}.json", optional: true, reloadOnChange: true)

Then in my original appsettings.json it would be something like:然后在我原来的appsettings.json它会是这样的:

"EmailSettings": {
    "Recipients": []
}

and then for local development ( Development environment): appsettings.Development.json然后用于本地开发( Development环境): appsettings.Development.json

"EmailSettings": {
    "Recipients": [
        "person@test.com"
    ]
}

and then for the other environment (lets call it Staging ): appsettings.Staging.json然后对于其他环境(我们称之为Staging ): appsettings.Staging.json

"EmailSettings": {
    "Recipients": [
        "anotherperson@test.com"
    ]
}

Then, just make sure in your additional environments you have set ASPNETCORE_ENVIRONMENT in environment variables.然后,只需确保在您的其他环境中已在环境变量中设置了ASPNETCORE_ENVIRONMENT

In the appsettings.Test.json use the followingappsettings.Test.json使用以下内容

"EmailSettings:Recipients:0" : "anotherperson@test.com"

The Configuration API is capable of maintaining hierarchical configuration data by flattening the hierarchical data with the use of a delimiter in the configuration keys.配置 API 能够通过使用配置键中的分隔符扁平化分层数据来维护分层配置数据。

You will need to define the Email Settings class like您将需要定义电子邮件设置类,如

   public class EmailSettings
    {
        public List<string> Recipients { get; set; }
    }

and wireup the DI to add the options in the Configure Method in Startup class并连接 DI 以在Startup类的Configure方法中添加选项

public void ConfigureServices(IServiceCollection services)
        {
            // add Options
            services.AddOptions();
            services.Configure<EmailSettings>(Configuration.GetSection("EmailSettings"));
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM