Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么TestServer(AspNetCore)在静态文件上给出404错误?_C#_Asp.net Core_.net Core Rc2 - Fatal编程技术网

C# 为什么TestServer(AspNetCore)在静态文件上给出404错误?

C# 为什么TestServer(AspNetCore)在静态文件上给出404错误?,c#,asp.net-core,.net-core-rc2,C#,Asp.net Core,.net Core Rc2,我有一个示例服务器 var host = new WebHostBuilder() .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) .UseIISIntegration() .UseStartup<Startup>() .Build();

我有一个示例服务器

var host = new WebHostBuilder()
                .UseKestrel()
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseIISIntegration()
                .UseStartup<Startup>()
                .Build();

            host.Run();
我正在使用xunit测试(学习):

一切正常(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从其他目录启动站点。我们必须这样解决它:

另一种方法是将这些文件复制到单元测试项目中

然后

只要您维护与正在测试的项目相同的结构文件夹,它就会找到文件并将其传递给依赖项注入过程

        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);
    public void Configure(IApplicationBuilder app)
    {
        app.UseDefaultFiles();
        app.UseStaticFiles();
        //app.Run(async (context) =>
        //{
        //    await context.Response.WriteAsync("Hello World!");
        //});
    }