1

I just started using .NET Core and JSON for my app settings files. I can see it's very easy to retrieve values from my appsettings.json file using the following...

IConfigurationRoot _config = new ConfigurationBuilder()
    .SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
    .Build();

// Determine from args the following...
// 1) Transport: FTP (default), WebAPI
// 2) Run as service: True, False (default)
if (!Enum.TryParse<TransportMethods>(_config.GetValue<string>("TransportMethod"), out TransportMethods tMethod))
tMethod = TransportMethods.FTP;

Now I would like the application to be able to save some "user settings" to the same appsettings.json file. What's the best way to save user settings to appsetting.json? From what I'm reading the IConfigurationRoot does not allow writing, it's read-only. So is there another way to save my settings to appsettings.json?

Arvo Bowen
  • 4,524
  • 6
  • 51
  • 109
  • All that I found so far is contamination of .NET from ASP.NET Web.config way of seeing configuration: [GitHub ASP.NET](https://github.com/aspnet/Configuration/issues/385) – amuliar Sep 02 '23 at 08:08

1 Answers1

0

You could use the custom configuration provider

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-2.2#custom-configuration-provider-1

That would give you a number of options such as storing the config in a database or a separate file.

zXynK
  • 1,101
  • 16
  • 30
  • Thanks for the input! I was hoping to avoid a DB right now. Just needed to save things like "the last path used" and "cached tokens", etc. When you say "or a separate file", can you give a small complete example on this answer of how to do that? The page you referenced is huge and I'm not exactly sure what I should be looking for. – Arvo Bowen Jan 19 '21 at 00:19
  • The key is to create a class that implements `IConfigurationSource`. The link shows an example of how you could use EF if you wanted. A quick google brings up some example of other methods too. Just out of interest, have you tried adding another `.AddJsonFile(...` to the builder with a different path? – zXynK Jan 19 '21 at 00:33
  • Not yet, but I was thinking about doing something like `.AddJsonFile("appsettings.local.json", optional: true, reloadOnChange: true)`. I don't think that would be a problem at all and I think it would read both files kind of merging them to get me one big JSON settings file. The issue is trying to write back to it. I thought Entity Framework was just for DB usage though? – Arvo Bowen Jan 19 '21 at 00:59
  • You're right, EF is for DB usage. Here is an example of using `IConfigurationSource` with a XML config: https://wildermuth.com/2018/04/15/building-a-net-core-configuration-source Maybe this could be a better solution? https://stackoverflow.com/questions/40970944/how-to-update-values-into-appsetting-json – zXynK Jan 19 '21 at 09:27
  • That's actually a SO question I started looking at yesterday. Though I'm geared toward JSON and trying to stay away from XML. "Trying to evolve" or something I guess... – Arvo Bowen Jan 19 '21 at 12:56
  • I've never tried using an absolute path with `AddJsonFile`. Maybe it could reference a file in the USERPROFILE directory. Something like this: https://stackoverflow.com/a/9993587/565633 – zXynK Jan 19 '21 at 14:18