How do I delete a node in appsettings.json using appsettings.Development.json?

时间秒杀一切 提交于 2020-01-05 04:14:11

问题


I am struggling a bit with how the appsettings.Development.json overrides or otherwise merges with the appsettings.json. I am not sure how to "clear" a node out of appsettings.json by using the appsettings.Development.json file.

For reference, I am using the default builder as seen here https://github.com/aspnet/MetaPackages/blob/rel/2.0.0-preview1/src/Microsoft.AspNetCore/WebHost.cs#L159-L160

appsettings.json

{
  "Policy": {
    "roles": [
      {
        "name": "inventoryAdmin",
        "subjects": [ "bob", "alice" ],
        "identityRoles": [ "ActiveDirectory-Role-Manager" ]
      },
  ]
 }
}

Given that example, why can I not do the following in my:

appsettings.Development.Json:

{ "Policy": { "Roles": [] } }

or

{ "Policy": { "Roles": null } }

When I check the output via something like Configuration.Get<PolicyServer.Local.Policy>().Roles I still get 3 roles back.

This question is hopefully going to guide me on how I can override a node and not just clear it. So I am hoping to start simple and work my way there.


回答1:


All of the settings that go into your IConfiguration instance are simply key-value pairs. Take the following, simplified example JSON:

{
    "Roles": [
        { "Name": "Role1", "Subjects": [ "Alice", "Bob" ] },
        { "Name": "Role2", "Subjects": [ "Charlie" ] }
    ]
}

Although this is essentially a tree structure, it maps into the following key-value pairs when added to your IConfiguration instance (there are some additional empty values here, but they're not part of this discussion):

  • Roles =
  • Roles:0:Name = Role1
  • Roles:0:Subjects:0 = Alice
  • Roles:0:Subjects:1 = Bob
  • Roles:1:Name = Role2
  • Roles:1:Subjects:0 = Charlie

You can see that this mimics the hierarchy of your JSON, where the names are object properties and the numbers are indexes into arrays. That first one is important: There's a key of Roles which has no value, because values can only be simple strings and its just a parent in itself.

Now, when you add an extra JSON file to the IConfiguration instance setup, it maps to a new set of key-value pairs that get applied on top of those that exist. Take the following additional JSON:

{
    "Roles": []
}

This simply overwrites the existing Roles key and sets it to, well, the same value it already has: nothing. The same applies if you use null in your JSON file - that's just how this stuff works.

In terms of a solution here, I suggest seeing if you can rework your appsettings.json approach. For example, you might be able to put the role configuration itself into e.g. an appsettings.Production.json file and leave the default version blank so that it doesn't exist in your development environment. In other words, try and model your different appsettings.json files to be additive themselves.



来源:https://stackoverflow.com/questions/52880484/how-do-i-delete-a-node-in-appsettings-json-using-appsettings-development-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!