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.
(Image: Pincasso / Shutterstock.com)
- 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.
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; }
}
(wpl)