How to update values into appsetting.json in asp.net application by using c# code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to update values into appsetting.json in asp.net application by using c# code?

I know this problem solution is so important so post qustions and their best answer with the code. If you have best answer for this qustions so please post on below answer section it's help lot's of programmers and me. Thank you

27th Jan 2022, 10:34 AM
Ashish Sharma
Ashish Sharma - avatar
1 Answer
0
it's simply run console application that reads application settings, adds a new setting, and updates an existing setting. and after update refresh the application on server without closed application. static void AddUpdateAppSettings(string key, string value) { try { var configFile = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~"); var settings = configFile.AppSettings.Settings; if (settings[key] == null) { settings.Add(key, value); } else { settings[key].Value = value; } configFile.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name); } catch (ConfigurationErrorsException ex) { Console.WriteLine("Error writing app settings. Error: "+ ex.Message); } }
27th Jan 2022, 10:37 AM
Ashish Sharma
Ashish Sharma - avatar