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

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

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

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

编辑:我错过了提到我确实尝试使用app.UseDefaultFiles(),如文档中提到但它不适合我。 服务器仍然返回404

该文档中的一条评论澄清了这一点:

Kieren_Johnstone精选2017年5月22日

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

这意味着,您应该编写类似这样的内容,以使您指定的文件夹能够提供默认页面:

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

来自文件 :

设置默认主页为网站访问者提供了访问您网站时的起点。 为了使您的Web应用程序在没有用户必须完全限定URI的情况下提供默认页面,请从Startup.Configure调用UseDefaultFiles扩展方法,如下所示。

 public void Configure(IApplicationBuilder app) { app.UseDefaultFiles(); app.UseStaticFiles(); // For the wwwroot folder app.UseStaticFiles(new StaticFileOptions() { FileProvider = new PhysicalFileProvider( Path.Combine(Directory.GetCurrentDirectory(), @"build")), RequestPath = new PathString("/app") }); } 

必须在UseStaticFiles之前调用UseDefaultFiles来提供默认文件。

用这个:

 public void Configure(IApplicationBuilder app) { // Serve my app-specific default file, if present. DefaultFilesOptions options = new DefaultFilesOptions(); options.DefaultFileNames.Clear(); options.DefaultFileNames.Add("mydefault.html"); app.UseDefaultFiles(options); app.UseStaticFiles(); } 

有关详细信息,请访问此链接:

 https://docs.microsoft.com/en-us/aspnet/core/fundamentals/static-files and go to section: "Serving a default document"