A minimal ASP.NET Core application

Profile pictureToni Petrina
Published on 2020-12-021 min read
  • #C#
  • #ASP.NET Core

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!"}


Change code theme: