小编gab*_*abs的帖子

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

我为每个环境都有自定义的 appsettings.json 文件,例如 appsettings.Dev.json、appsettings.Test.json、appsettings.Prod.json。在主 appsettings.json 中,我有以下代码:

  "EmailSettings": {
    "Recipients": [
      "person@test.com"
    ]
  }
Run Code Online (Sandbox Code Playgroud)

然后在自定义 json 文件中,我想覆盖此列表,如下所示:

  "EmailSettings": {
    "Recipients": [
      "anotherperson@test.com"
    ]
  }
Run Code Online (Sandbox Code Playgroud)

但相反,它会像这样附加:

  "EmailSettings": {
    "Recipients": [
      "person@test.com",
      "anotherperson@test.com"
    ]
  }
Run Code Online (Sandbox Code Playgroud)

对于所有其他类型的设置,它们都会被替换,但由于某种原因,似乎会附加自定义设置文件中的列表。在 .net 中,您可以使用 xslt 获得更细粒度的信息,以便能够确定是否要替换或附加覆盖的设置。这里有什么建议吗?

解决方案(对我来说)

我这样做了,它在自定义 json 设置中被替换。主要appsettings.json:

  "EmailSettings": {
    "Recipients:0": "person@test.com"
  }
Run Code Online (Sandbox Code Playgroud)

然后在自定义设置文件中:

  "EmailSettings": {
    "Recipients:0": "anotherperson@test.com"
  }
Run Code Online (Sandbox Code Playgroud)

感谢您的回复!

c# json appsettings asp.net-core

5
推荐指数
1
解决办法
1658
查看次数

标签 统计

appsettings ×1

asp.net-core ×1

c# ×1

json ×1