如何从 .Net 5 (Asp.Net) 中的 appsettings.json 映射多级设置 [英] How to map multiple levels of Settings from appsettings.json in .Net 5 (Asp.Net)
问题描述
我正在将一个 Asp.Net MVC 应用程序迁移到 .Net5.我有一个静态类作为 web.config 中设置的外观.我的 Setting 类暴露了静态属性,每个类代表设置组,例如:
公共静态类 MySettings{公共静态类 MainDB{公共静态字符串 ConnectionString{得到{//获取实际值的代码返回";}}}公共静态类 ExternalApi{公共静态字符串 Uri{得到{//检索实际值的代码返回";}}}公共静态类 OtherSettings{//...}}
在 .Net Core 5(实际上,从 .Net Core 2 开始)我们使用 POCO 的 tyo 读取设置,如 https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-5.0
有什么方法可以将我的所有设置映射到一个对象,例如,对于 appsettings.json:
<代码>{记录":{日志级别":{默认":信息",微软":警告",Microsoft.Hosting.Lifetime":信息"}},允许的主机":*",设置":{主数据库":{ConnectionString":访问数据库所需的任何内容"},ExtrenaAPI"{Uri":https://whatever.api.com",密钥":mysupersecretkey",秘密":mysupersecret-uh-secret"}}}
课程:
公共类 MainDB{公共字符串 ConnectionString { 获取;放;}}公共类 ExternalApi{公共字符串 Uri { get;放;}公共字符串密钥 { 获取;放;}公共字符串秘密{得到;放;}}公开课设置{公共主数据库主数据库 { 获取;放;}`public ExternalApi ExternalApi { 获取;放;}}
配置(在 Startup.cs 中):
services.Configure(Configuration.GetSection(Settings"));
(是的,我知道我可以做 services.Configure
和 services.Configure
但如果可能的话,我想将所有设置都放在一个单一对象中.
有什么建议吗?
我在这里假设您在谈论绑定(应用设置文件到单个对象?)如果您可以接受一些额外的代码,那么这可以满足您想要实现的目标.
IConfigurationRoot configRoot = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();设置 settingsRoot = new Settings();//带有 appsettings 结构的自定义 POCO((IConfiguration)configRoot).Bind(settingsRoot);公开课设置{公共日志记录 { get;放;}公共字符串 AllowedHosts { 获取;放;}}公共类 LogLevel{公共字符串默认{获取;放;}公共字符串 Microsoft { get;放;}}
如果您只想设置单个节点/部分的层次结构,您可以简单地执行一个 ((IConfiguration)config.GetSection("SectionName")).Bind(myObject)
>
无论哪种方式,config.Bind(object)
都是这里的魔法部分.
I am migrating an Asp.Net MVC application to .Net5. I had a static class that served as a façade for the settings in web.config. My Setting class exposed static properties, each one of a class representing settings groups, for example:
public static class MySettings
{
public static class MainDB
{
public static string ConnectionString
{
get
{
// Code to retrieve the actual values
return "";
}
}
}
public static class ExternalApi
{
public static string Uri
{
get
{ // Code to retrieve the actual values
return "";
}
}
}
public static class OtherSettings
{
// ...
}
}
In .Net Core 5 (actually, since .Net Core 2) we use POCO's tyo read settings as depicted in https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-5.0
Is there any way to map all my settings to one single object, for example, for appsettings.json:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"Settings": {
"MainDB": {
"ConnectionString": "whatever needed to access the db"
},
"ExtrenaAPI" {
"Uri": "https://whatever.api.com",
"Key": "mysupersecretkey",
"Secret": "mysupersecret-uh-secret"
}
}
}
Classes:
public class MainDB
{
public string ConnectionString { get; set; }
}
public class ExternalApi
{
public string Uri { get; set; }
public string Key { get; set; }
public string Secret { get; set; }
}
public class Settings
{
public MainDB MainDB { get; set; }
`public ExternalApi ExternalApi { get; set; }
}
Configuration (in Startup.cs):
services.Configure<Settings>(Configuration.GetSection("Settings"));
(Yes, I know I can do services.Configure<MainDB>(Configuration.GetSection("Settings:MainDB"));
and services.Configure<ExternalApi>(Configuration.GetSection("Settings:ExternalApi"));
but I'd like to get all the settings in one sigle object if it is possible.
Any suggestion?
Am assuming here you're talking about binding (app settings file to a single object?) If you're okay with bit of extra code then this could work for what you want to achieve.
IConfigurationRoot configRoot = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
Settings settingsRoot = new Settings(); // custom POCO with appsettings structure
((IConfiguration)configRoot).Bind(settingsRoot);
public class Settings
{
public Logging Logging { get; set; }
public string AllowedHosts { get; set; }
}
public class LogLevel
{
public string Default { get; set; }
public string Microsoft { get; set; }
}
If you're only trying to setup a single node/section's hierarchy you can simply do a ((IConfiguration)config.GetSection("SectionName")).Bind(myObject)
Either way config.Bind(object)
is the magic bit here.
这篇关于如何从 .Net 5 (Asp.Net) 中的 appsettings.json 映射多级设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!