为什么TestServer(AspNetCore)在静态文件上会出现404错误?我有一个示例服务器varhost=newWebHostBuilder().UseKestrel().UseContentRoot(Directory.GetCurrentDirectory()).UseIISIntegration().UseStartup().Build();主机.Run();在Startup类中配置:publicvoidConfigure(IApplicationBuilderapp){app.Run(async(context)=>{awaitcontext.Response.WriteAsync("HelloWorld!");});我正在使用xunit测试(学习):publicTestFixture(){varbuilder=newWebHostBuilder().UseStartup();_server=newTestServer(建造者);客户端=_server.CreateClient();Client.BaseAddress=newUri(地址);}然后varresponse=awaitClient.GetAsync("http://localhost:51021/");Assert.Equal(HttpStatusCode.OK,response.StatusCode);varresponseString=awaitresponse.Content.ReadAsStringAsync();varcontent=awaitresponse.Content.ReadAsStringAsync();断言。包含(“你好世界!”,内容);一切都很好(200)。现在我正在更改Startup.cspublicvoidConfigure(IApplicationBuilderapp){app.UseDefaultFiles();app.UseStaticFiles();//app.Run(async(context)=>//{//awaitcontext.Response.WriteAsync("HelloWorld!");//});如果我用浏览器启动应用程序,一切正常(显示index.html)。但是如果我用TestServer调用它,我会收到(404Notfound)。我的错误在哪里?XUnit从不同的目录启动站点。我们要这样解决:https://github.com/aspnet/StaticFiles/blob/d692066b2bd711653150ad2cccc2268583355532/test/Microsoft.AspNetCore.StaticFiles.Tests/StaticFilesTestServer.cs#L20以上是C#学习教程:WhyTestServer(AspNetCore)在静态文件上出现404错误?如果所有分享的内容对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
