Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core appsettings.json update 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<AppSettings>(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<AppSettings> 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.

like image 318
Siemko Avatar asked Jan 14 '17 18:01

Siemko


People also ask

Can we update Appsettings json?

json programmatically. You have to overwrite the appsettings. json file to be able to update values programmatically.

What is IConfiguration in ASP.NET Core?

The IConfiguration is an interface for . Net Core 2.0. The IConfiguration interface need to be injected as dependency in the Controller and then later used throughout the Controller. The IConfiguration interface is used to read Settings and Connection Strings from AppSettings. json file.

What is IConfiguration C#?

Get<T>(IConfiguration) Attempts to bind the configuration instance to a new instance of type T. If this configuration section has a value, that will be used.


2 Answers

Basically you can set the values in IConfiguration like this:

IConfiguration configuration = ... // ... configuration["key"] = "value"; 

The issue there is that e.g. the JsonConfigurationProvider does not implement the saving of the configuration into the file. As you can see in the source it does not override the Set method of ConfigurationProvider. (see source)

You can create your own provider and implement the saving there. Here (Basic sample of Entity Framework custom provider) is an example how to do it.

like image 60
Mathias Avatar answered Oct 14 '22 08:10

Mathias


Here is a relevant article from Microsoft regarding Configuration setup in .Net Core Apps:

Asp.Net Core Configuration

The page also has sample code which may also be helpful.

Update

I thought In-memory provider and binding to a POCO class might be of some use but does not work as OP expected.

The next option can be setting reloadOnChange parameter of AddJsonFile to true while adding the configuration file and manually parsing the JSON configuration file and making changes as intended.

    public class Startup     {         ...         public Startup(IHostingEnvironment env)         {             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();         }         ...     } 

... reloadOnChange is only supported in ASP.NET Core 1.1 and higher.

like image 21
Ankit Avatar answered Oct 14 '22 08:10

Ankit