Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core - serving static files [duplicate]

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>?

like image 583
Matthew Layton Avatar asked Jan 31 '18 22:01

Matthew Layton


People also ask

Which of the following static files can ASP.NET Core applications serve?

Static files, such as HTML, CSS, images, and JavaScript, are assets an ASP.NET Core app serves directly to clients by default.

Which of the following Middlewares must be installed to serve static files in ASP.NET Core application?

To serve static files from an ASP.NET Core app, you must configure static files middleware. With static files middleware configured, an ASP.NET Core app will serve all files located in a certain folder (typically /wwwroot).


1 Answers

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.

like image 188
bin Avatar answered Oct 24 '22 07:10

bin