A minimal ASP.NET Core application

December 02, 2020

With the most recent C# 9 feature top-level statement, a simple ASP.NET Core server can be written in just one file:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Hosting;

    .ConfigureWebHostDefaults(webBuilder =>
        webBuilder.Configure(app =>
            app.UseEndpoints(endpoints =>
                endpoints.MapGet("/", async context =>
                    await context.Response.WriteAsJsonAsync(new {
                        Message = "Hello world!"

Of course, a csproj file is still needed:

<Project Sdk="Microsoft.NET.Sdk.Web">



Running the app with dotnet run and opening http://localhost:5000 yields:

{"message":"Hello world!"}