配置要在根请求上提供的默认文件
正如我们在之前章节部分中了解到的,app.UseDefaultFiles() 中间件为根请求提供以下文件。
- Default.html
- Default.htm
- Index.html
- Index.htm
假设你想将 home.html 作为默认页面,应该在根访问上显示。为此,请在 UseDefaultFiles 方法中指定 DefaultFilesOptions,如下所示。
public class Startup{public void Configure(IApplicationBuilder app, IHostingEnvironment env){DefaultFilesOptions options = new DefaultFilesOptions();options.DefaultFileNames.Clear();options.DefaultFileNames.Add("home.html");app.UseDefaultFiles(options);app.UseStaticFiles();app.Run(async (context) =>{await context.Response.WriteAsync("Hello World");});}}
现在,http://localhost:<端口> 地址将显示根请求上 wwwroot 文件夹中的 home.html。