我想问一下如何创建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实例?
发布评论