I wrote the question on how to write a simple static file server for .NET Core using as few lines and files as possible.
It got marked as duplicate of ASP.NET Core - serving static files. So my question is:
Is this answer really the simplest .NET Core static file webserver possible? Have I understood it correctly (I am trying to piece together information from all kinds of sources)?
Startup.cs
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
namespace StaticFileServer {
    public class Startup{
        public void Configure(IApplicationBuilder app){
            app.UseDefaultFiles();
            app.UseStaticFiles(new StaticFileOptions() 
            {
                FileProvider = new PhysicalFileProvider(
                    Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot\dist"))
            });
        }
    }
}
Main.cs
using System;
using Microsoft.AspNetCore.Hosting;
namespace StaticFileServer
{
    class Program
    {
        static void Main(string[] args)
        {    
            var host = new WebHostBuilder()
            .UseKestrel()
            .UseStartup<Startup>()
            .Build();
            host.Run();
        }
    }
}
Putting the codes together, the simplest complete application I have found for serving static files from that location is the following:
class Program
{
    static void Main(string[] args)
    {
        WebHost.CreateDefaultBuilder(args)
            .Configure(config => config.UseStaticFiles())
            .UseWebRoot("wwwroot/dist").Build().Run();
    }
}
If you are trying to keep things light, you can include Microsoft.AspNetCore, Microsoft.AspNetCore.Hosting and Microsoft.AspNetCore.StaticFiles. Or for quick setups, you can just install the one package Microsoft.AspNetCore.All. 
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With