How to update values into appsetting.json?

前端 未结 4 2121
半阙折子戏
半阙折子戏 2020-11-27 12:39

I am using the IOptions pattern as described in the official documentation.

This works fine when I am reading values from appsetting.json,

4条回答
  •  我在风中等你
    2020-11-27 13:34

    public static void SetAppSettingValue(string key, string value, string appSettingsJsonFilePath = null) {
     if (appSettingsJsonFilePath == null) {
      appSettingsJsonFilePath = System.IO.Path.Combine(System.AppContext.BaseDirectory, "appsettings.json");
     }
    
     var json = System.IO.File.ReadAllText(appSettingsJsonFilePath);
     dynamic jsonObj = Newtonsoft.Json.JsonConvert.DeserializeObject < Newtonsoft.Json.Linq.JObject > (json);
    
     jsonObj[key] = value;
    
     string output = Newtonsoft.Json.JsonConvert.SerializeObject(jsonObj, Newtonsoft.Json.Formatting.Indented);
    
     System.IO.File.WriteAllText(appSettingsJsonFilePath, output);
    }
    

提交回复
热议问题