Обновление ASP.NET Core appsettings.json в коде ⇐ C#
-
Anonymous
Обновление ASP.NET Core appsettings.json в коде
I am currently working on project using asp.net core v1.1, and in my appsettings.json I have:
"AppSettings": { "AzureConnectionKey": "***", "AzureContainerName": "**", "NumberOfTicks": 621355968000000000, "NumberOfMiliseconds": 10000, "SelectedPvInstalationIds": [ 13, 137, 126, 121, 68, 29 ], "MaxPvPower": 160, "MaxWindPower": 5745.35 }, I also have class that I use to store them:
public class AppSettings { public string AzureConnectionKey { get; set; } public string AzureContainerName { get; set; } public long NumberOfTicks { get; set; } public long NumberOfMiliseconds { get; set; } public int[] SelectedPvInstalationIds { get; set; } public decimal MaxPvPower { get; set; } public decimal MaxWindPower { get; set; } } And DI enabled to use then in Startup.cs:
services.Configure(Configuration.GetSection("AppSettings")); Is there any way to change and save MaxPvPower and MaxWindPower from Controller?
I tried using
private readonly AppSettings _settings; public HomeController(IOptions settings) { _settings = settings.Value; } [Authorize(Policy = "AdminPolicy")] public IActionResult UpdateSettings(decimal pv, decimal wind) { _settings.MaxPvPower = pv; _settings.MaxWindPower = wind; return Redirect("Settings"); } But it did nothing.
Источник: https://stackoverflow.com/questions/416 ... te-in-code
I am currently working on project using asp.net core v1.1, and in my appsettings.json I have:
"AppSettings": { "AzureConnectionKey": "***", "AzureContainerName": "**", "NumberOfTicks": 621355968000000000, "NumberOfMiliseconds": 10000, "SelectedPvInstalationIds": [ 13, 137, 126, 121, 68, 29 ], "MaxPvPower": 160, "MaxWindPower": 5745.35 }, I also have class that I use to store them:
public class AppSettings { public string AzureConnectionKey { get; set; } public string AzureContainerName { get; set; } public long NumberOfTicks { get; set; } public long NumberOfMiliseconds { get; set; } public int[] SelectedPvInstalationIds { get; set; } public decimal MaxPvPower { get; set; } public decimal MaxWindPower { get; set; } } And DI enabled to use then in Startup.cs:
services.Configure(Configuration.GetSection("AppSettings")); Is there any way to change and save MaxPvPower and MaxWindPower from Controller?
I tried using
private readonly AppSettings _settings; public HomeController(IOptions settings) { _settings = settings.Value; } [Authorize(Policy = "AdminPolicy")] public IActionResult UpdateSettings(decimal pv, decimal wind) { _settings.MaxPvPower = pv; _settings.MaxWindPower = wind; return Redirect("Settings"); } But it did nothing.
Источник: https://stackoverflow.com/questions/416 ... te-in-code
Мобильная версия