New in .NET 10.0 [18]: Web server as a file-based app

In a file-based app, you can start the Kestrel web server based on ASP.NET Core.

listen Print view
Traffic sign with inscription .NET

(Image: Pincasso / Shutterstock.com)

1 min. read
By
  • Dr. Holger Schwichtenberg

Microsoft calls the direct translation and startup of C# files File-based Apps. It is also possible to start a Kestrel web server within such an app. The example below shows an ASP.NET Core Minimal WebAPI as a standalone C# file. Hosting is done in the web server Kestrel supplied with ASP.NET Core.

The Dotnet Doctor – Holger Schwichtenberg
Der Dotnet-Doktor – Holger Schwichtenberg

Dr. Holger Schwichtenberg is the technical director of the expert network www.IT-Visions.de, which supports numerous medium-sized and large companies with consulting and training services as well as software development, drawing on the expertise of 53 renowned experts. Thanks to his appearances at numerous national and international conferences, as well as more than 90 specialist books and over 1,500 specialist articles, Holger Schwichtenberg is one of the best-known experts for .NET and web technologies in Germany.

In the file-based app, Microsoft.NET.Sdk.Web is specified as the SDK. The NuGet packages used are Microsoft.AspNetCore.OpenApi and Humanizer. Ahead-of-Time compilation, which is standard in file-based apps, is deactivated here to avoid warnings from the JSON serializer.

Videos by heise

The following code shows the web server based on ASP.NET Core with a minimal WebAPI endpoint and OpenAPI metadata:

#:sdk Microsoft.NET.Sdk.Web
#:package Microsoft.AspNetCore.OpenApi@10.*-*
#:package Humanizer@2.14.1
#:property Version=1.1.4
#:property PublishAot=false

using Humanizer;
using Microsoft.OpenApi;

// Webserver einrichten
var builder = WebApplication.CreateBuilder();
builder.Services.AddOpenApi();
var app = builder.Build();
app.MapOpenApi(); // http://localhost:5000/openapi/v1.json

app.MapGet("/", () =>
 {
     // Daten fĂĽr Operation
     var d = new Data
     {
         Version = "9.0",
         Release = "2024-11-12"
     };
     var dotNet9Released = DateTimeOffset.Parse(d.Release);
     var since = DateTimeOffset.Now - dotNet9Released;

     return $"It has been {since.Humanize()} since .NET {d.Version} was released.";
 });

app.MapGet("/data", () =>
 {
     var d = new Data
     {
         Version = "9.0",
         Release = "2024-11-12"
     };
     return Results.Json(d);
 });

app.Run();

class Data
{
    /// <summary>
    /// Version of the .NET release.
    /// </summary>
    public string? Version { get; set; }
    /// <summary>
    /// Release date of the .NET version.
    /// </summary>
    public string? Release { get; set; }
}

Start and output of the web server (Fig. 1)

(wpl)

Don't miss any news – follow us on Facebook, LinkedIn or Mastodon.

This article was originally published in German. It was translated with technical assistance and editorially reviewed before publication.