。NET核心设置-最佳做法

问题描述 投票:-1回答:2

在.NET Framework中,我曾经使用过“属性”-“设置”(Properties.Settings.Default.SomeSettingsValue)中的设置。这非常容易使用。我现在开始使用.NET Core,但是没有这样的设置。我找到了一些解决方案,但是没有一个解决方案像旧的解决方案那样简洁明了。

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

谢谢。

c# .net asp.net-core .net-core configuration
2个回答
1
投票

我认为您正在寻找Options pattern

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

0
投票

您应该使用ASP.Net Core Configuration API。例如,您可以将配置设置添加到appsettings.json文件,该文件通常在首次创建时自动添加到您的项目中,例如:

{
    "Logging": {
        "LogLevel": {
            "Default": "Information"
        }
    },
    "ConnectionStrings": {
        "MyDatabase": "Server=Server1;Database=MyDatabase;Integrated Security=false;",
    },
}
© www.soinside.com 2019 - 2024. All rights reserved.