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
{
"Policy": {
"roles": [
{
"name": "inventoryAdmin",
"subjects": [ "bob", "alice" ],
"identityRoles": [ "ActiveDirectory-Role-Manager" ]
},
]
}
}
Given that example, why can I not do the following in my:
{ "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.
json programmatically. You have to overwrite the appsettings. json file to be able to update values programmatically.
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.
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.
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.
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.
{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?
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.
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With