Starting a http file server from the file explorer using .NET Core 2.0 and Kestrel

 
 
  • Gérald Barré

The previous version of this post used .NET Core 1.1. This post covers the same approach with .NET Core 2.0. The main difference is the web server startup method.

When developing HTML/JS applications and testing locally (file:///c:/...), your browser will often block certain features such as reading local files via XMLHttpRequest. In Google Chrome, you can disable this restriction with the --allow-file-access-from-files command-line argument, but that compromises your browser's security. A better approach is to run a lightweight web server that serves static files. Since I need to start one frequently, I added a shortcut to the Windows Explorer context menu.

Final resultFinal result

  1. Create a .NET Core console application
  2. Reference the following NuGet packages:
    • Microsoft.AspNetCore
    • Microsoft.AspNetCore.StaticFiles
    • Microsoft.Win32.Registry
  3. Create the web server with default files and directory browsing
C#
using System;
using System.IO;
using System.Reflection;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore;
using Microsoft.Win32;

class Program
{
    public static void Main(string[] args)
    {
        BuildWebHost(args).Run();
    }

    public static IWebHost BuildWebHost(string[] args)
    {
        var path = Directory.GetCurrentDirectory();
        if (args.Length > 0)
        {
            path = args[0];
        }

        Environment.CurrentDirectory = path;
        return WebHost.CreateDefaultBuilder()
            .UseStartup<Startup>()
            .UseKestrel()
            .UseContentRoot(path)
            .UseWebRoot(path)
            .UseUrls("http://localhost:5000")
            .Build();
    }
}

public class Startup
{
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        var fileServerOptions = new FileServerOptions
        {
            EnableDefaultFiles = true,
            EnableDirectoryBrowsing = true,
            FileProvider = env.WebRootFileProvider
        };
        fileServerOptions.StaticFileOptions.ServeUnknownFileTypes = true;
        app.UseFileServer(fileServerOptions);
    }
}

Once the application is running, you can browse the directory contents at http://localhost:5000. Each request is logged to the console for debugging purposes.

If you are on Windows, you can add a context menu entry in Windows Explorer to start the web server directly from any folder:

C#
static void RegisterContextMenu()
{
    string location = Assembly.GetEntryAssembly().Location;
    using (var key = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Classes\Directory\shell\FileServer"))
    {
        key.SetValue("", "Open with FileServer", RegistryValueKind.String);
        key.SetValue("Icon", location, RegistryValueKind.String);

        using (var commandKey = key.CreateSubKey("command"))
        {
            if (location.EndsWith(".dll"))
            {
                commandKey.SetValue("", "\"C:\\Program Files\\dotnet\\dotnet.exe\" \"" + location + "\" \"%V\"", RegistryValueKind.String);
            }
            else
            {
                commandKey.SetValue("", "\"" + location + "\" \"%V\"", RegistryValueKind.String);
            }
        }
    }
}

Call this method at application startup:

C#
public static void Main(string[] args)
{
    RegisterContextMenu();
    BuildWebHost(args).Run();
}

Do you have a question or a suggestion about this post? Contact me!

Follow me:
Enjoy this blog?