使用 appsettings.Production.json 中的数组设置覆盖 appsettings.json 中的数组设置
Override array settings in appsettings.json with those in appsettings.Production.json
我正在使用 ASP.NET Core 2.1。我在 appsettings.json
中有设置,我使用选项模式将它们绑定到 类。我想在 appsettings.Production.json
.
中覆盖其中的一些
根据文档支持覆盖,并且通常对我有用。
但它不适用于数组。
appsettings.json
:
"MySectionOuter": {
"MySectionInner": [
{
"foo": "1",
"bar": "2",
"baz": "3"
},
{
"foo": "a",
"bar": "b",
"baz": "c"
}
]
}
我在 appsettings.Production.json
中的覆盖
"MySectionOuter": {
"MySectionInner": [
{
"bar": "4",
},
{
"baz": "d"
}
]
}
但是这不起作用 - 它添加而不是替换。
I 数组是键值存储的语法糖。所以我也试过这个:
"MySectionOuter": {
"MySection:1": {
"bar": "4",
},
"MySection:2": {
"baz": "b",
}
}
但这也行不通。
正确的语法是什么?
更新
评论说明我没有解释清楚。我想要的是这样的:
开发中:
element1: foo=1
element1: bar=2
element1: baz=3
element2: foo=a
element2: bar=b
element2: baz=c
制作中:
element1: foo=1
element1: bar=2
element1: baz=4 // this was changed
element2: foo=a
element2: bar=b
element2: baz=d // this was changed
根据此博客 post:https://www.paraesthesia.com/archive/2018/06/20/microsoft-extensions-configuration-deep-dive/
无法删除提供程序的配置项。
You can add configuration at override time, but you can’t remove things. The best you can do is override a value with an empty string.
相反,您应该只在 appsettings.config
中填写所需的信息,并在更专门的设置文件中填写适当的设置。例如。 appsettings.Development.config
或您的 appsettings.Production.config
。或者按照博客中的建议 post:
Since you can’t remove things, specify as little configuration as possible and behave correctly using defaults in your code when configuration isn’t there.
实际上,构建配置时并没有数组。它只是一个键值对字典。所以你最终得到了字符串键,比如
"mysectionouter:mysectioninner:0:foo" = 1
.
因此,当您在配置中定义数组时,会发生以下情况:
appsettings.json:
"mysectionouter:mysectioninner:0:foo" = 1
"mysectionouter:mysectioninner:0:bar" = 2
appsettings.production.json:
"mysectionouter:mysectioninner:0:bar" = new1
结果:
foo = 1
bar = new1
所以它只是基于索引的,下一个配置只是覆盖一个键。在您的第二个示例中,除了更改索引外,您一无所获。代表将是:
"mysectionouter:mysectioninner:1:bar" = new1
所以回到你的问题:数组在应用程序设置中很棘手,虽然支持,但通常难以使用且不直观。
通过索引,您可能会得到两个不相关对象的奇怪合并,如果您在文件中定义不同的设置集,例如第一个配置中的设置 A 和 B,第二个配置中的 C,您将得到 C 和B 在结果中,你可能根本不想有 B。更糟糕的是,如果您只定义每个对象的某些字段,您可能会混合使用 A 和 C。
我建议使用其他一些文件来存储此类信息。您还可以在加载配置的地方中断调试器,亲眼看看这些密钥是如何构建的,以获得更多的洞察力。
我正在使用 ASP.NET Core 2.1。我在 appsettings.json
中有设置,我使用选项模式将它们绑定到 类。我想在 appsettings.Production.json
.
根据文档支持覆盖,并且通常对我有用。 但它不适用于数组。
appsettings.json
:
"MySectionOuter": {
"MySectionInner": [
{
"foo": "1",
"bar": "2",
"baz": "3"
},
{
"foo": "a",
"bar": "b",
"baz": "c"
}
]
}
我在 appsettings.Production.json
"MySectionOuter": {
"MySectionInner": [
{
"bar": "4",
},
{
"baz": "d"
}
]
}
但是这不起作用 - 它添加而不是替换。
I
"MySectionOuter": {
"MySection:1": {
"bar": "4",
},
"MySection:2": {
"baz": "b",
}
}
但这也行不通。
正确的语法是什么?
更新
评论说明我没有解释清楚。我想要的是这样的:
开发中:
element1: foo=1
element1: bar=2
element1: baz=3
element2: foo=a
element2: bar=b
element2: baz=c
制作中:
element1: foo=1
element1: bar=2
element1: baz=4 // this was changed
element2: foo=a
element2: bar=b
element2: baz=d // this was changed
根据此博客 post:https://www.paraesthesia.com/archive/2018/06/20/microsoft-extensions-configuration-deep-dive/
无法删除提供程序的配置项。
You can add configuration at override time, but you can’t remove things. The best you can do is override a value with an empty string.
相反,您应该只在 appsettings.config
中填写所需的信息,并在更专门的设置文件中填写适当的设置。例如。 appsettings.Development.config
或您的 appsettings.Production.config
。或者按照博客中的建议 post:
Since you can’t remove things, specify as little configuration as possible and behave correctly using defaults in your code when configuration isn’t there.
实际上,构建配置时并没有数组。它只是一个键值对字典。所以你最终得到了字符串键,比如
"mysectionouter:mysectioninner:0:foo" = 1
.
因此,当您在配置中定义数组时,会发生以下情况:
appsettings.json:
"mysectionouter:mysectioninner:0:foo" = 1
"mysectionouter:mysectioninner:0:bar" = 2
appsettings.production.json:
"mysectionouter:mysectioninner:0:bar" = new1
结果:
foo = 1
bar = new1
所以它只是基于索引的,下一个配置只是覆盖一个键。在您的第二个示例中,除了更改索引外,您一无所获。代表将是:
"mysectionouter:mysectioninner:1:bar" = new1
所以回到你的问题:数组在应用程序设置中很棘手,虽然支持,但通常难以使用且不直观。
通过索引,您可能会得到两个不相关对象的奇怪合并,如果您在文件中定义不同的设置集,例如第一个配置中的设置 A 和 B,第二个配置中的 C,您将得到 C 和B 在结果中,你可能根本不想有 B。更糟糕的是,如果您只定义每个对象的某些字段,您可能会混合使用 A 和 C。
我建议使用其他一些文件来存储此类信息。您还可以在加载配置的地方中断调试器,亲眼看看这些密钥是如何构建的,以获得更多的洞察力。