C# 从.NET Core 2.1升级到3.1后,视图不再是';当我的应用程序在E2E测试中启动时找不到
在C# 从.NET Core 2.1升级到3.1后,视图不再是';当我的应用程序在E2E测试中启动时找不到,c#,asp.net-core,C#,Asp.net Core,在Home控制器中,我返回返回视图(“登录”,返回URL) 在Views/Home/中有Login.cshtml 当我手动启动应用程序时,一切正常,但当我在Selenium测试中启动应用程序时: [Parallelizable] [Test] public async Task TEST() { var info = this.ConnectionInfo[Configure()]; foreach (var driver in GetDrivers()) {
Home
控制器中,我返回返回视图(“登录”,返回URL)代码>
在Views/Home/
中有Login.cshtml
当我手动启动应用程序时,一切正常,但当我在Selenium测试中启动应用程序时:
[Parallelizable]
[Test]
public async Task TEST()
{
var info = this.ConnectionInfo[Configure()];
foreach (var driver in GetDrivers())
{
var _context = GetDb(info.ConnectionString);
driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(15);
_context.Database.EnsureDeleted();
_context.Database.EnsureCreated();
driver.Navigate().GoToUrl(info.Address);
}
}
public Guid Configure()
{
var addressId = Guid.NewGuid();
var port1 = FreeTcpPort();
var port2 = FreeTcpPort();
var dbFileGuid = Guid.NewGuid().ToString("N") + ".db";
var config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
config["Tests:ConnectionString"] = $"Filename={dbFileGuid}";
_host = new WebHostBuilder()
.UseKestrel()
.UseConfiguration
(
config
)
.UseUrls($"http://localhost:{port1}/", $"https://localhost:{port2}/")
.UseStartup<TestsStartup>()
.UseSerilog()
.Build();
_host.StartAsync();
ConnectionInfo.TryAdd(addressId, new ConnectionInfo($"http://127.0.0.1:{port1}", dbFileGuid, _host));
return addressId;
}
是什么原因导致该应用程序在从测试启动时无法找到这些视图,同时又设法在较旧的.NET Core/ASP libs上找到了这些视图?嗯
我通过告诉Test
project将Views
文件夹从应用程序文件夹复制到Test的bin
这就是我添加到.csproj
<ItemGroup>
<Content Include="..\MyApp\Views\**" Link="Views\%(RecursiveDir)%(Filename)%(Extension)" CopyToOutputDirectory="Always" />
</ItemGroup>
更改启动文件中的两项内容:
(将MVC添加到项目管道中)
然后像这样更改端点:(这是MVC项目的默认路由)
您可以更改视图属性(生成操作,复制到输出目录)。这可能会解决您的问题,屏幕附在此处:
<ItemGroup>
<Content Include="..\MyApp\Views\**" Link="Views\%(RecursiveDir)%(Filename)%(Extension)" CopyToOutputDirectory="Always" />
</ItemGroup>
services
.AddControllersWithViews(x =>
{
x.Filters.Add(typeof(CustomExceptionFilter));
})
.AddNewtonsoftJson()
.AddApplicationPart(typeof(HomeController).Assembly)
.AddMvc().AddControllersAsServices();
.SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
app.UseEndpoints(endpoints =>
{
endpoints.MapDefaultControllerRoute();
});