读取或访问.net Core中的自定义配置扩展

我已根据以下文章构建了自定义配置(提供者和服务)。 https://medium.com/@dneimke/custom-configuration-in-net-core-2-193ff6f02046

现在,当要在使用DI时从IConfiguration访问自定义添加的提供程序时。

我在创业公司中的Configuration看起来像

读取或访问.net Core中的自定义配置扩展

它显示有6个提供程序,标记的第5个索引是我的自定义配置。

在控制器中注入IConfiguration时,如何访问我添加的自定义配置(第5个索引)?

读取或访问.net Core中的自定义配置扩展

需要从自定义配置中获取连接和连接设置的值

TIA

sunxiangmin 回答:读取或访问.net Core中的自定义配置扩展

例如,您可以从IConfigurationRoot.Providers获取所有配置提供程序

public class HomeController : Controller
{
    private readonly IConfiguration _configuration = null;
    private readonly ApplicationDbContext _dbContext;

    public HomeController(IConfiguration configuration,ApplicationDbContext dbContext)
    {
        _configuration = configuration;
        _dbContext = dbContext;
    }

    public IActionResult Index()
    {
        var configurationRoot = _configuration as IConfigurationRoot;

        var provider = (DbProvider)configurationRoot.Providers.Last();

       // Or get specific provider using index as below comment has shown
       // var list = configurationRoot.Providers.ToList();
       // var provider = (DbProvider)list[5];

        var connectionSettings = provider.connectionSettings;

        return View();
    }
}
本文链接:https://www.f2er.com/3167230.html

大家都在问