本文介绍了如何在本地创建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);

这很有意义,因为它是接口,但是在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();

有关更多信息,请参见

For further information see Configuration in ASP.NET Core

这篇关于如何在本地创建IConfiguration实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 04:57