.NET Core 设置 - 最佳实践

Ban*_*ake 5 .net c# configuration .net-core asp.net-core

在 .NET Framework 中,我习惯于使用属性 - 设置 ( Properties.Settings.Default.SomeSettingsValue) 中的设置。使用起来非常简单和干净。我现在开始使用.NET Core,但是没有任何这样的设置。我找到了一些解决方案,但没有一个像旧的那样干净简单。

.NET Core 中设置的最佳实践和解决方案是什么?

谢谢。

Mik*_*der 4

我认为您正在寻找选项模式

  1. 将设置添加到 appsettings.json:“setting1”:“测试设置”。
  2. 创建一个类: public class AppSettings {
    public string Setting1 { get; set; } }
  3. 将其添加services.Configure<AppSettings>(Configuration);到您的startup.cs
  4. 现在您可以注入IOptions<AppSettings> appSettings您需要这些选项的类