Search code examples
c#asp.net-corestatic-files

ASP.NET Core - serving static files


I'm following this documentation but I'm getting stuck: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/static-files

Consider my directory structure:

wwwroot
    dist
        index.html

In my startup class, I have:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }

    app.UseDefaultFiles();
    app.UseStaticFiles();
    app.UseStaticFiles(new StaticFileOptions
    {
        FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "dist"))
    });
}

When I start the application I don't see my index.html page, but I do if I navigate to <host>/dist/index.html

How can I configure this so that ASP.NET automatically takes me to that page from <host>?


Solution

  • You'll have create middleware or a URL rewrite to do the work for you. ASP.NET Core isn't the smartest and it isn't going to manually do stuff for you.

    You should also be doing WebHostBuillder.UseWebRoot(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "dist")) in your Program.cs file.

    Also, this looks like a duplicate of this.