I am currently working on project using asp.net core v1.1, and in my appsettings.json I have:
\"AppSettings\": {
\"AzureConnectionKey\": \"***\",
\"Azu
{
Config: {
IsConfig: false
}
}
Main(){
AddOrUpdateAppSetting("Config:IsConfig", true);
}
public static void AddOrUpdateAppSetting(string key, T value) {
try {
var filePath = Path.Combine(AppContext.BaseDirectory, "appSettings.json");
string json = File.ReadAllText(filePath);
dynamic jsonObj = Newtonsoft.Json.JsonConvert.DeserializeObject(json);
var sectionPath = key.Split(":")[0];
if (!string.IsNullOrEmpty(sectionPath)) {
var keyPath = key.Split(":")[1];
jsonObj[sectionPath][keyPath] = value;
}
else {
jsonObj[sectionPath] = value; // if no sectionpath just set the value
}
string output = Newtonsoft.Json.JsonConvert.SerializeObject(jsonObj, Newtonsoft.Json.Formatting.Indented);
File.WriteAllText(filePath, output);
}
catch (ConfigurationErrorsException) {
Console.WriteLine("Error writing app settings");
}
}