我正在编写一个集成测试,使用WebApplicationFactory 来测试我的APIs。
我已经创建了一个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 代码?