在.NET Framework中,我曾经使用过“属性”-“设置”(Properties.Settings.Default.SomeSettingsValue
)中的设置。这非常容易使用。我现在开始使用.NET Core,但是没有这样的设置。我找到了一些解决方案,但是没有一个解决方案像旧的解决方案那样简洁明了。
。NET Core中的设置的最佳实践和解决方案是什么?
谢谢。
我认为您正在寻找Options pattern
public class AppSettings
{
public string Setting1 { get; set; }
}
services.Configure<AppSettings>(Configuration);
添加到您的startup.csIOptions<AppSettings> appSettings
您应该使用ASP.Net Core Configuration API。例如,您可以将配置设置添加到appsettings.json文件,该文件通常在首次创建时自动添加到您的项目中,例如:
{
"Logging": {
"LogLevel": {
"Default": "Information"
}
},
"ConnectionStrings": {
"MyDatabase": "Server=Server1;Database=MyDatabase;Integrated Security=false;",
},
}