Static Files

Live on this server

Serves files from a directory or resource tree.

Serves the files of a folder, for example downloads or the assets of an application. GenHTTP sends each file with the content type of its extension, and Defaults() adds compression and lets browsers cache the files.

Put your files into a files folder next to the program, so /README.md serves files/README.md. Assets.From("./files") is a shorter way to serve a folder from disk.

To run it, create a console app (dotnet new console), add GenHTTP (dotnet add package GenHTTP.Full), replace Program.cs with the code below and start it with dotnet run - it answers at http://localhost:8080/.

Program.cs13 lines
using GenHTTP.Engine.Internal;

using GenHTTP.Modules.Files;
using GenHTTP.Modules.IO;
using GenHTTP.Modules.Practices;

// GET /README.md serves files/README.md
var app = Assets.From(ResourceTree.FromDirectory("./files"));

await Host.Create()
          .Handler(app)
          .Defaults()
          .RunAsync();

Try it

Send the requests to the sample running on this server - change the path, headers or body as you like, or copy them as curl commands.

  1. Fetch a file

    GET
    curl -i 'https://genhttp.org/demo/static-files/README.md'
  2. Fetch another one

    GET
    curl -i 'https://genhttp.org/demo/static-files/books.csv'