绑定appsettings.json中的子配置以键入

二元组

我试图弄清楚如何将我的appsettings.json的子部分绑定到类类型。

public class EmailProviderSettings : IEmailProviderSettings
{
    public string PopServer { get; set; }
    public int PopPort { get; set; }
    public string SmtpServer { get; set; }
    public int SmtpPortTls { get; set; }
    public int SmtpPortSsl { get; set; }
    public string ApiKey { get; set; }
    public bool UseSsl { get; set; }
    public string UserId { get; set; }
    public string UserPassword { get; set; }
    public string SentFromName { get; set; }
    public string SentFromEmail { get; set; }
}

appsettings.json

{
"ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=92533D3BF281;Trusted_Connection=True;MultipleActiveResultSets=true"
},  
  "EmailConfigurations": {
     "Gmail": {
      "ApiKey": "",
      "UseSsl": true,
      "UserId": "[email protected]",
      "Password": "metootoo!",
      "SentFromName": "joe blo",
      "SentFromEmail": "[email protected]",
      "PopServer": "pop.gmail.com",
      "PopPort": 995,
      "SmtpServer": "smtp.gmail.com",
      "SmtpPortSsl": 465,
      "SmtpPortTls": 587
    },
    "SendGrid": {
      "ApiKey": "SG.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
      "UseSsl": true,
      "UserId": "",
      "Password": "",
      "SentFromName": "Joe BLo",
      "SentFromEmail": "[email protected]",
      "PopServer": "",
      "PopPort": "",
      "SmtpServer": "smtp.sendgrid.com",
      "SmtpPortSsl": 465,
      "SmtpPortTls": 587
    }
  } 
}

在控制器中

 private readonly IConfiguration _config;

 public HomeController(IConfiguration config)
 {
     _config = config;
 }

并在ActionResult中

//my last effort which doesnt work
var providerSettings = new EmailProviderSettings();  
_config.GetSection("EmailConfigurations").GetSection("SendGrid").Bind(providerSettings);

现在如何将设置绑定到EmailProviderSettings的实例?此尝试的错误是

IndexOutOfRangeException:索引超出数组的范围。

在六月

在您的代码中,您尝试将SendGrid部分中的“ SendGrid”绑定到选项。

我想你的意思是:

var configSection = _config.GetSection("EmailConfigurations").GetSection("SendGrid");
var settings = new EmailProviderSettings();
configSection.Bind(settings);

这会将SendGrid部分绑定到您的POCO。

另外,最好int将可以为空成员设为int?

public int? PopPort { get; set; }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

绑定xbindkeysrc中的键以键入命令

将appsettings.json中的部分绑定到选项

绑定来自appsettings.json的字符串列表

将 appsettings.json 部分绑定到 POCO 列表

无法理解与.NET Core中与Appsettings绑定相关联的行为的问题

Microsoft.Azure.WebJobs.Host:无法绑定参数“ myContext”以键入DataContext。Azure Function v2中的错误

appsettings.json 中的配置类

從 appsettings.json 配置文件中讀取

在 appsettings.json GitHubActions 中更改配置值

尝试从appsettings.json中读取Serilog配置

如何从.NET Core中的appsettings.json配置嵌套对象?

从注入的配置类访问 AppSettings.json 中的顶级字段

从 appsettings.json 获取 ConnectionStrings 配置设置

使用appsettings.json配置DbContext映射

Core xUnit Test Project中的配置:找不到配置文件“ appsettings.test.json”

JtextArea中的键绑定以获取键入的文本

在 Nancy 中阅读 appsettings

C#将整个appSettings文件绑定到类

如何从.net Core 2.2中的appsettings.json中读取日志配置

如何在Azure函数的AppSettings.json中添加配置值。有什么结构吗?

如何在Angular 10中读取appsettings.json文件配置

ASP.NET Core Web服务不会将appsettings.json加载到配置中

如何在 appsettings.json 中配置 serilog-sinks-slack

如何在控制台应用程序中处理新格式(appsettings.json)的配置

如何在nuget软件包中包含appsettings.json配置设置?

在AppSettings.json中为Serilog接收器MsSqlServer配置列选项

Web.Config AppSettings中的Json

在IIS中覆盖appsettings.json

在appsettings.json中重复元素