Configure the Default File to be Served on the Root Request

As we learned in the Set Default File section, app.UseDefaultFiles() middleware serves the following files on the root request.

  1. Default.html
  2. Default.htm
  3. Index.html
  4. Index.htm

Suppose, you want to set home.html as a default page which should be displayed on the root access. To do that, specify DefaultFilesOptions in the UseDefaultFiles method as shown below.

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");
        });
    }
}

Now, this will display home.html from wwwroot folder on the root request http://localhost:<port>.