# Tiny dotnet web api

With top level statements in c#9 and dotnet 5 there is really little code needed to have a working web api.

Prerequisite: Download and install .Net 5 SDK (opens new window).

Over in your terminal:

# Make a .Net project file
> touch tiny-web-api.csproj

# Make a c# entrypoint for our api
> touch Program.cs

In the project file, tell .Net about the sdk and .Net-version we want to use.

<!-- tiny-web-api.csproj -->

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>
</Project>

Now we can build the web api in c#.

// Program.cs

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

WebHost
    .CreateDefaultBuilder()
    .Configure(app => 
        app.Run(httpContext => 
            httpContext.Response.WriteAsync("I'm a tiny api")))
    .Build()
    .Run();

Thats it, we are done 🙌

Let's run the api from the terminal

> dotnet run

it will output something like

> dotnet run

Hosting environment: Production
Content root path: /path/to/your/code
Now listening on: http://localhost:5000
Now listening on: https://localhost:5001
Application started. Press Ctrl+C to shut down.

To see it working, visit localhost:5000 (opens new window) in you browser, or from the command line with curl.

> curl http://localhost:5000
I'm a tiny api

👉 Filip W (opens new window) wrote this up over at his blog (opens new window), and this is basically my TL;DR of that post. Go there for more, Phil writes really well about web develoment 👏

Last updated: April 15th 2024