image.png

One file API with C# in .net 10

#dotnet #Net10 #minimalApi #RestAPI #WebDevelopment #CSharp

What

.NET 10 lets you create APIs with just one C# file using `dotnet run app.cs`. No .sln or .csproj needed

Why

I needed a quick API for a workaround while working with n8n. FastAPI worked great, but I wondered - can .NET do something similar with minimal setup?

How

Need:

.NET 10 (preview 4+) and a text editor

Create app.cs:

csharp
#:sdk Microsoft.NET.Sdk.Web 
var builder = WebApplication.CreateBuilder(); 
var app = builder.Build(); 
app.MapGet("/", () => "Hello World!"); 
app.Run();

Run:

dotnet run app.cs Where #:sdk is the only new thing - "#:" prefix makes c# compiler ignore this line, and "sdk" defines which SDK we want to use (Microsoft.Net.Sdk.Web this time, because we want to expose API endpoint, instead of default Microsoft.Net.Sdk).

Perfect for quick prototypes and learning. No scaffolding, no fuss.