我有一个示例服务器
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
在 Startup 类中进行配置:
public void Configure(IApplicationBuilder app)
{
app.Run(async (context) =>
{
await context.Response.WriteAsync("Hello World!");
});
}
我正在使用 xunit 测试(学习):
public TestFixture()
{
var builder = new WebHostBuilder().UseStartup<TStartup>();
_server = new TestServer(builder);
Client = _server.CreateClient();
Client.BaseAddress = new Uri(address);
}
然后
var response = await Client.GetAsync("http://localhost:51021/");
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var responseString = await response.Content.ReadAsStringAsync();
var content = await response.Content.ReadAsStringAsync();
Assert.Contains("Hello World!", content);
一切都很好(200)。现在我正在更改 Startup.cs
public void Configure(IApplicationBuilder app)
{
app.UseDefaultFiles();
app.UseStaticFiles();
//app.Run(async (context) =>
//{
// await context.Response.WriteAsync("Hello World!");
//});
}
如果我使用浏览器启动应用程序,一切正常(显示index.html)。但是,如果我使用 TestServer 调用它,我会收到(404 未找到)。我的错误在哪里?
XUnit 从不同的目录启动站点。我们必须像这样解决这个问题:https://github.com/aspnet/StaticFiles/blob/d692066b2bd711653150ad2cccc2268583355532/test/Microsoft.AspNetCore.StaticFiles.Tests/StaticFilesTestServer.cs#L20
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)