Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 537
Victorio Berra Avatar asked Oct 18 '18 18:10

Victorio Berra


People also ask

Can we update Appsettings json?

json programmatically. You have to overwrite the appsettings. json file to be able to update values programmatically.

What is Appsettings development json?

The appsettings. json file is generally used to store the application configuration settings such as database connection strings, any application scope global variables, and much other information.

Can we have multiple Appsettings json?

Of course, we can add and use multiple appsettings. json files in ASP.NET Core project. To configure and read data from your custom json files, you can refer to the following code snippet.

Can I move Appsettings json out of the app directory?

Yes, I'm leaving appsettings. json in the project and it's being deployed - for documentation and non-sensitive settings. And, yes, I could leave sensitive data encrypted in Visual Studio Online but for now, the simplest thing to do is hide an overriding version elsewhere in the file system, as I'm doing.

What is appsettings JSON in ASP NET Core?

The appsettings.json file is an application configuration file used to store configuration settings such as database connections strings, any application scope global variables, etc. If you open the ASP.NET Core appsettings.json file, then you see the following code by default which is created by visual studio.

What is ASP NET environment JSON file?

{Environment}.json, where the {Environment} is the nothing but the applications current hosting environments such as Development, Staging or Production) What is ASP.NET Core appsettings.json File?

How do I add a JSON file to an existing project?

To add it to any project: Add new item. Name it appsettings.json. Initialize it as an empty JSON file: Make it copy appsettings.json to the output folder.

How do I change the value of a JSON config object?

Change the values in the config object as desired: Use the built-in System.Text.Json serializer to serialize the config object. Use the desired serialization settings. You’ll probably want the JSON indented in appsettings.json, so set WriteIndented=true:


1 Answers

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.

like image 188
Kirk Larkin Avatar answered Nov 15 '22 08:11

Kirk Larkin