我目前正在编写一个集成测试来使用 WebApplicationFactory 测试我的 API。
我创建了一个 CustomWebApplicationFactory。我想阅读在主 Web 项目中找到的默认 appsettings.json。我知道这可以实现如下:
private static readonly IConfigurationRoot _configuration; // Global
// Inside Constructor
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", true, true)
_configuration = builder.Build();
// To get connectionstring
var connStr = _configuration.GetConnectionString("DefaultConnection");
就我而言,我想默认使用 appsettings.json 。有没有一种方法可以通过从主 Web 项目中读取 appsettings.json 来检索连接字符串,而无需在构造函数中定义 AddJsonFile 代码?