Setting index.html as default page in asp.net core

I needed to declare UseDefaultFiles() before UseStaticFiles().

app.UseDefaultFiles();
app.UseStaticFiles();

app.UseDefaultFiles(new DefaultFilesOptions {
    DefaultFileNames = new List<string> { "index.html" }
});
app.UseStaticFiles();

This is optimal since the UseDefaultFiles URL rewriter will only search for index.html, and not for the legacy files: default.htm, default.html, and index.htm.

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-2.2#serve-a-default-document


Feb 2022 Update

To serve static files (such as index.html), the files should be in the folder wwwroot in your project. Use UseWebRoot if you want to change it to a different folder.

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseFileServer();
}

This method also accepts a FileServerOptions instance for even more fine tuned control such as enabling directory browsing, which is disabled by default.

Reference: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/static-files

Original answer (Jan '18)

Install the NuGet package Microsoft.AspNetCore.StaticFiles.

Now, in Startup.Configure method, add:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    // Serve the files Default.htm, default.html, Index.htm, Index.html
    // by default (in that order), i.e., without having to explicitly qualify the URL.
    // For example, if your endpoint is http://localhost:3012/ and wwwroot directory
    // has Index.html, then Index.html will be served when someone hits
    // http://localhost:3012/
    //
    // (Function 1)
    app.UseDefaultFiles();

    // Enable static files to be served. This would allow html, images, etc. in wwwroot
    // directory to be served.
    //
    // (Function 2)
    app.UseStaticFiles();
}

Note: The order in which these functions are called is important. In OO programming, it's quite hard not to depend on ordering as objects maintain states that can vary during the lifetime of the object. (You guessed it right, one solution to prevent designs like this is to implement immutability.)

You should now get files served from wwwroot directory (use UseWebRoot if you want to change it to something else).

Source: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/static-files


Just use this in startup.cs:

app.UseFileServer();

It's shorthand for:

app.UseDefaultFiles();
app.UseStaticFiles();

it avoids issues with having to have those in the correct order (as shown above)