Skip to main content

ASP.NET Core–Serve a default.html

Sometimes you lose waaay to much time on something that looks obvious once you find it. Today I was searching a solution to serve a default.html file when the root url was called in Asp.NET Core; e.g. http://mysamplesite/root/

I was first fiddling around with the StaticFiles middleware but couldn’t find a way. Turns out there is another middleware; DefaultFiles that handles this use case.

Just call the UseDefaultFiles method from Startup.Configure:

public void Configure(IApplicationBuilder app)
{
  app.UseDefaultFiles();
  app.UseStaticFiles();
}

With UseDefaultFiles, requests to a folder search for:

  • default.htm
  • default.html
  • index.htm
  • index.html

The first file found from the list is served as though the request were the fully qualified URI. The browser URL continues to reflect the URI requested.

Remark: UseDefaultFiles must be called before UseStaticFiles to serve the default file.

More information: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-2.1