简体   繁体   English

将值保存到 appsetting..NET 核心中的 json

[英]Saving values to appsetting.json in .NET Core

I just started using .NET Core and JSON for my app settings files.我刚开始使用 .NET Core 和 JSON 作为我的应用程序设置文件。 I can see it's very easy to retrieve values from my appsettings.json file using the following...我可以看到使用以下命令从我的 appsettings.json 文件中检索值非常容易......

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.现在我希望应用程序能够将一些“用户设置”保存到相同的 appsettings.json 文件中。 What's the best way to save user settings to appsetting.json?将用户设置保存到 appsetting.json 的最佳方法是什么? From what I'm reading the IConfigurationRoot does not allow writing, it's read-only.从我正在阅读的内容来看, IConfigurationRoot 不允许写入,它是只读的。 So is there another way to save my settings to appsettings.json?那么还有另一种方法可以将我的设置保存到 appsettings.json 吗?

You could use the custom configuration provider您可以使用自定义配置提供程序

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-2.2#custom-configuration-provider-1 https://docs.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.这将为您提供许多选项,例如将配置存储在数据库或单独的文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM