Based on @Alper Ebicoglu answer
GET:
// ===== || GET || GET appsettings.js property =====================================================================
[HttpGet]
[Route("GetNotificationDays")]
public async Task<IActionResult> GetNotificationDays()
{
var path = System.IO.Path.Combine(Directory.GetCurrentDirectory(), "appsettings.json");
var json = await System.IO.File.ReadAllTextAsync(path);
dynamic jsonObj = Newtonsoft.Json.JsonConvert.DeserializeObject<Newtonsoft.Json.Linq.JObject>(json);
return StatusCode(200, new { daysBefore = (int)jsonObj.InvoicementNotificationSettings.DaysBefore});
}
Exp:
(int)jsonObj.InvoicementNotificationSettings.DaysBefore =
(int) = cast to int - depending on the property
jsonObj = appsettings.js,
InvoicementNotificationSettings = object in appsettings.js,
DaysBefore = property in InvoicementNotificationSettings
UPDATE: appsettings.js
// ===== || PUT || UPDATE appsettings.js property =====================================================================
[HttpPut]
[Route("SetNotificationDays")]
public async Task<IActionResult> SetNotificationDays(int notificationDays)
{
if (notificationDays != 0)
{
var path = System.IO.Path.Combine(Directory.GetCurrentDirectory(), "appsettings.json");
var json = await System.IO.File.ReadAllTextAsync(path);
dynamic jsonObj = Newtonsoft.Json.JsonConvert.DeserializeObject<Newtonsoft.Json.Linq.JObject>(json);
jsonObj.InvoicementNotificationSettings.DaysBefore = notificationDays;
string output = Newtonsoft.Json.JsonConvert.SerializeObject(jsonObj, Newtonsoft.Json.Formatting.Indented);
await System.IO.File.WriteAllTextAsync(path, output);
return await GetNotificationDays();
}
return StatusCode(409);
}
If reading appsettings from memmory:
Ex: int daysBefore = configuration.GetValue<int>("InvoicementNotificationSettings:DaysBefore");
Than In Startup.js - to autoreload appsettings.js after updating
public class Startup
{
public static IConfiguration Configuration { get; set; }
// Constructor -----------------------------------------------------------------------------------------------------------------------------
public Startup(IConfiguration configuration, Microsoft.Extensions.Hosting.IHostEnvironment env)
{
Configuration = configuration;
// To autoreload appsettings.js after update -------------------------
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
appsettings.js
{
"ConnectionStrings": {
"DefaultConnection": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=ItlCrmsDb;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"
},
"InvoicementNotificationSettings": {
"DaysBefore": 4
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}