指定 asp.net core 静态文件夹的默认文件名

Arv*_*man 6 c# asp.net asp.net-mvc asp.net-core

我目前在一个文件夹中生成了一个生成的 index.html、js 和其他静态文件,我将该文件夹标记为静态文件夹(通过在 Startup.cs 的 Configure 方法中添加以下内容:

   app.UseDefaultFiles();
   app.UseStaticFiles(new StaticFileOptions()
    {
        FileProvider = new Path.Combine(env.ContentRootPath, @"../build")),
        RequestPath = new PathString("/app/")
    });
Run Code Online (Sandbox Code Playgroud)

有没有办法将 index.html 设置为这个 */app 路由的默认响应?因为现在 localhost:5000/app/ 返回 404 而 localhost:5000/app/index.html 返回 index.html。

编辑:我没有提到我确实尝试使用文档中提到的 app.UseDefaultFiles() ,但它对我不起作用。服务器仍然返回 404

小智 5

文档中的评论之一对此进行了澄清:

Kieren_Johnstone 精选 2017 年 5 月 22 日

“提供默认文档”部分遗漏了一些重要信息。如果您将 UseStaticFiles 配置为处理非根 RequestPath,则似乎需要将相同的 FileProvider 和 RequestPath 传递给 UseDefaultFiles 和 UseStaticFiles。您不能总是按照本节中的说明调用它。

所以这意味着,你应该写这样的东西来使你指定的文件夹能够提供一个默认页面:

    app.UseDefaultFiles(new DefaultFilesOptions()
    {
        FileProvider = Path.Combine(env.ContentRootPath, @"../build")),
        RequestPath = new PathString("/app/")
    });
    app.UseStaticFiles(new StaticFileOptions()
    {
        FileProvider = Path.Combine(env.ContentRootPath, @"../build")),
        RequestPath = new PathString("/app/")
    });
Run Code Online (Sandbox Code Playgroud)