我想问一下如何创建 ASP.NET Core 配置的实例,这与我在知道 appsettings.json
的 Controller 构造函数中需要它时创建的实例相同。文件
喜欢 _config = ActivatorUtilities.CreateInstance<IConfiguration>(null);
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.'
这是有道理的,因为它是接口(interface),但它在 Controller 的 Constructor 案例中是如何工作的呢?我怎样才能创建一个实例?
我正在使用 MS DI
谢谢
最佳答案
您可以创建一个本地配置实例,如下所示。
IConfigurationRoot configuration = new ConfigurationBuilder()
.SetBasePath([PATH_WHERE_appsettings.json_RESIDES])
.AddJsonFile("appsettings.json")
.Build();
如需更多信息,请参阅 Configuration in ASP.NET Core
关于c# - 如何在本地创建 IConfiguration 实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56732820/