diff --git a/src/Microsoft.AspNetCore.Mvc.Testing/WebApplicationFactory.cs b/src/Microsoft.AspNetCore.Mvc.Testing/WebApplicationFactory.cs index 3e31e9b5aa..c04fe25f45 100644 --- a/src/Microsoft.AspNetCore.Mvc.Testing/WebApplicationFactory.cs +++ b/src/Microsoft.AspNetCore.Mvc.Testing/WebApplicationFactory.cs @@ -128,6 +128,11 @@ namespace Microsoft.AspNetCore.Mvc.Testing private void SetContentRoot(IWebHostBuilder builder) { + if (SetContentRootFromSetting(builder)) + { + return; + } + var metadataAttributes = GetContentRootMetadataAttributes( typeof(TEntryPoint).Assembly.FullName, typeof(TEntryPoint).Assembly.GetName().Name); @@ -161,6 +166,24 @@ namespace Microsoft.AspNetCore.Mvc.Testing } } + private static bool SetContentRootFromSetting(IWebHostBuilder builder) + { + // Attempt to look for TEST_CONTENTROOT_APPNAME in settings. This should result in looking for + // ASPNETCORE_TEST_CONTENTROOT_APPNAME environment variable. + var assemblyName = typeof(TEntryPoint).Assembly.GetName().Name; + var settingSuffix = assemblyName.ToUpperInvariant().Replace(".", "_"); + var settingName = $"TEST_CONTENTROOT_{settingSuffix}"; + + var settingValue = builder.GetSetting(settingName); + if (settingValue == null) + { + return false; + } + + builder.UseContentRoot(settingValue); + return true; + } + private WebApplicationFactoryContentRootAttribute[] GetContentRootMetadataAttributes( string tEntryPointAssemblyFullName, string tEntryPointAssemblyName)