如何在本地创建IConfiguration实例?

编程入门 行业动态 更新时间:2024-10-19 18:23:21
本文介绍了如何在本地创建IConfiguration实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想问一下如何创建ASP.NET Core配置实例,该实例与我在Controller的构造函数中需要它时所创建的实例相同,该构造函数知道 appsettings.json 文件

I'd want to ask how to create an instance of ASP.NET Core's Configuration, the same that's being created when I require it in Controller's constructor which knows about the appsettings.json file

像 _config = ActivatorUtilities.CreateInstance< IConfiguration>(null);

System.InvalidOperationException:'找不到类型为'Microsoft.Extensions.Configuration.IConfiguration'的合适的构造函数。确保类型是具体的,并且为公共构造函数的所有参数注册了服务。'

System.InvalidOperationException: 'A suitable constructor for type 'Microsoft.Extensions.Configuration.IConfiguration' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor.'

这很有意义,因为它是接口,但是在Controller的Constructor案例中如何工作?以及如何创建该实例?

It makes sense because it is interface, but how it does work in Controller's Constructor case? and how can I create an instance of that?

我正在使用MS DI

I'm using MS DI

谢谢

推荐答案

您可以创建本地配置实例,如下所示。

You can create a local instance of configuration as shown below.

IConfigurationRoot configuration = new ConfigurationBuilder() .SetBasePath([PATH_WHERE_appsettings.json_RESIDES]) .AddJsonFile("appsettings.json") .Build();

有关更多信息,请参见在ASP.NET Core中进行配置

For further information see Configuration in ASP.NET Core

更多推荐

如何在本地创建IConfiguration实例?

本文发布于:2023-11-14 15:04:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1587689.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实例   如何在   IConfiguration

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!