Minimal Minimum API

Minimal Minimum API

In this article we will create an api using the new .net-6 feature called the "Minimum API" and use an absolute minimum of files we can get away with to create an example. To follow along you will need .Net-6 installed on your computer:

Create a new folder called mmapi, open it in your editor of choice such as VS Code. Now create a new file and call it minimal.csproj. This is our project file written in XML which is used to configure our project. These files can get big on some projects but for this example we just need to specify the framework we are targeting. So copy the following XML into the file:

<Project Sdk="Microsoft.NET.Sdk.Web">

? ? <PropertyGroup>

? ? ? ? <TargetFramework>net6.0</TargetFramework>

? ? ? ? <ImplicitUsings>enable</ImplicitUsings>

? ? </PropertyGroup>

</Project>        

We will also need a file to hold our code for our hello world example though with this being a minimum app we will make it a hi ??, this will be called program.cs. The program.cs file is the default file to be run in dotnet and is where the code to initialise and application goes.?Copy the following code into the program.cs file:

var builder = WebApplication.CreateBuilder(args);

var app = builder.Build();

app.MapGet("/", () => “Hi ??!");

app.Run();        

In 4 lines of code we have created an app that will handle get requests and return Hello World. The above example also shows another dotnet6 feature called?top level statements, this allows us to avoid the normal boiler plate code of defining a class and a static main method and dive straight into the code we want to run for this example.

To see this in action go into a command line and build the code with:

 dotnet build
 dotnet run        

You will see a message similar to: Now listening on:?https://localhost:5000?appear on the command line, view that url in a browser and?you will see Hi ??! appear.?

要查看或添加评论,请登录

?? Andy S.的更多文章

  • Dotnet Containers - now and when

    Dotnet Containers - now and when

    .Net has evolved over the years from being exclusively for windows then through the work of the mono project its become…

  • String Theory - DotNet7

    String Theory - DotNet7

    In dotnet 7 strings are getting some love with a few improvements that will make devs life a little easier. Dotnet 7…

  • 1000Km of commuting by bike

    1000Km of commuting by bike

    After several months of cycling on a regular basis I have hit racked up 1000 kilometres on my bike. This is a good time…

    4 条评论
  • My health and fitness

    My health and fitness

    I've decided to take better care of myself and so am increasing the aerobic and strength fitness I do. So this week I…

  • Cycling to work - the kit list

    Cycling to work - the kit list

    I have started regularly commuting to work on bike, it's not much longer time wise than driving, gives me some exercise…

  • The 1 Minute?Pitch

    The 1 Minute?Pitch

    It’s Friday, the start of the Startup Weekend, you’ve chatted, ate tasty pizza, heard tales of toil and triumph from a…

  • Startup weekend kit list

    Startup weekend kit list

    These are only suggestions, but here are a few recommendations to get the most out of a Startup Weekend. Water bottle:…

  • Smoke, Startups and Mirrors

    Smoke, Startups and Mirrors

    Startup Weekends normally involve several projects with a technical based idea that ultimately wants a website or app…

    1 条评论
  • Get involved in Derbys first Startup Weekend

    Get involved in Derbys first Startup Weekend

    Derby is hosting its first ever Startup Weekend on March the 10th to the 12th. Derby with its huge pool of talented…

  • My First Startup Weekend

    My First Startup Weekend

    My first Startup Weekend was in Nottingham, put on by Nottinghams amazing Creative Quarter team, and I loved it. It was…

社区洞察

其他会员也浏览了