din*_*tom 10 configuration asp.net-core-2.0
我正在尝试学习检索配置信息的各种方法,以便我可以确定为即将开始的项目设置和使用配置的最佳途径.
我可以使用访问各种单一设置
var sm = new SmsSettings
{
FromPhone = Configuration.GetValue<string>("SmsSettings:FromPhone"),
StartMessagePart = Configuration.GetValue<string>("SmsSettings:StartMessagePart"),
EndMessagePart = Configuration.GetValue<string>("SmsSettings:EndMessagePart")
};
Run Code Online (Sandbox Code Playgroud)
我还需要能够计算设置,确定某些设置的值等.所以我正在构建一个解析方法来执行这些类型的事情,并且需要设置文件的整个部分,这就是我假设的GetSection所做的.错误.
appsettings文件
{
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=TestingConfigurationNetCoreTwo;Trusted_Connection=True;MultipleActiveResultSets=true",
"ProductionConnection": "Server=(localdb)\\mssqllocaldb;Database=TestingConfigurationNetCoreTwo_Production;Trusted_Connection=True;MultipleActiveResultSets=true"
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
}
},
"SmsSettings": {
"FromPhone": "9145670987",
"StartMessagePart": "Dear user, You have requested info from us on starting",
"EndMessagePart": "Thank you."
}
}
Run Code Online (Sandbox Code Playgroud)
以下是两个截图
var section = Configuration.GetSection("ConnectionStrings");
Run Code Online (Sandbox Code Playgroud)
回报
出现了一些问题.
Tej*_*tel 19
我知道答案已被接受。但是,提供正确的示例代码,以防万一有人想了解更多...
绑定自定义强类型配置非常简单。IE。配置json如下所示
{
"AppSettings": {
"v": true,
"SmsSettings": {
"FromPhone": "9145670987",
"StartMessagePart": "Dear user, You have requested info from us on starting",
"EndMessagePart": "Thank you."
},
"Auth2Keys": {
"Google": {
"ClientId": "",
"ClientSecret": ""
},
"Microsoft": {
"ClientId": "",
"ClientSecret": ""
},
"JWT": {
"SecretKey": "",
"Issuer": ""
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
你的 C# 类看起来像
public class SmsSettings{
public string FromPhone { get; set;}
public string StartMessagePart { get; set;}
public string EndMessagePart { get; set;}
}
public class ClientSecretKeys
{
public string ClientId { get; set; }
public string ClientSecret { get; set; }
}
public class JWTKeys
{
public string SecretKey { get; set; }
public string Issuer { get; set; }
}
public class Auth2Keys
{
public ClientSecretKeys Google { get; set; }
public ClientSecretKeys Microsoft { get; set; }
public JWTKeys JWT { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
您可以GetSection("sectionNameWithPath")通过调用获取该部分,然后通过调用转换为强类型Get<T>();
var smsSettings = Configuration.GetSection("AppSettings:SmsSettings").Get<SmsSettings>();
var auth2Keys= Configuration.GetSection("AppSettings:Auth2Keys").Get<Auth2Keys>();
Run Code Online (Sandbox Code Playgroud)
对于简单的字符串值
var isDebugMode = Configuration.GetValue("AppSettings:IsDebugMode");
希望这可以帮助...
根据这篇文章
https://github.com/aspnet/Configuration/issues/716
GetSection("Name").Value将返回null,则必须使用GetChildren获得的子项Bind 可能不适用于您的对象,请确保属性为 publicGet<T>()绑定,它将为您提供配置对象的强类型实例尝试使用您班级的简单POCO(无复杂的getter / setter,所有公共方法,无方法),然后从那里获取
如果GetSections()与一起使用Bind(),则应该能够创建供您使用的poco对象。
var poco= new PocoClass();
Configuration.GetSection("SmsSettings").Bind(poco);
Run Code Online (Sandbox Code Playgroud)
这将为您返回一个已设置所有值的poco对象。
小智 6
如果您对 GetSection 返回的对象使用 Bind 方法,那么这会将节中的键值对绑定到它所绑定的对象的相应属性。
例如,
class ConnectionStrings {
public string DefaultConnection { get; set;}
public string ProductionConnection {get; set;}
}
Run Code Online (Sandbox Code Playgroud)
..
var connectionStrings = new ConnectionStrings();
var section = Configuration.GetSection("ConnectionStrings").Bind(connectionStrings);
Run Code Online (Sandbox Code Playgroud)