Skip to content

From FeatherHttp to Minimal Apis

Photo by <a href="https://unsplash.com/@isaacbenhesed?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Isaac Benhesed</a>
Photo by Isaac Benhesed

A while a ago, I blogged about how I liked the FeatherHttp Project by David Fowler.

Now it looks like Asp.Net Dev Team have plans to include it as part of .NET 6.

While watching a an opening talk by Scott Hunter at Devintersection. I saw a demonstration of Minimal Web Apis. You can view the demos here.

After a bit more googling with Bing 😄. I found an ASP.NET Community Standup Stream which discusses the origins of Minimal apis and the plans the development team have for them. Towards the end of the video there is a discussion on the RequestDelegateFactory which looks really interesting.

The big sellling point here is less code.

so taking code written using FeatherHttp

using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;

namespace FeatherHttpDemo
{
    internal class Program
    {
        private static async Task Main(string[] args)
        {
            var app = WebApplication.Create(args);

            app.MapGet("/api/info", async http =>
            {
                var apiInfo = new
                {
                    Title = "FeatherHttp Api Demo",
                    Version = "1.0.0",
                    Copyright = "Eamonn Flynn 2020"
                };

                await http.Response.WriteJsonAsync(apiInfo);
            });

            await app.RunAsync();
        }
    }
}

can be written using C# 10 as

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

var builder = WebApplication.CreateBuilder(args);
await using var app = builder.Build();

 app.MapGet("/api/info", async http =>
        {
            var apiInfo = new
            {
                Title = "Minimal Api Demo",
                Version = "1.0.0",
                Copyright = "Eamonn Flynn 2021"
            };

            await http.Response.WriteAsJsonAsync(apiInfo);
        });

await app.RunAsync();

N.B at the time of writing this post. You need to be running a Preview of .Net 6 for the above code to work.

As I have said before, even with using the dotnet new command line, setting up an Asp.Net Web Api requires a bit more ceremony and understanding compared to other server frameworks.

Minimal Apis look to reduce that bar to entry and help get more projects going on .Net Core

References

ASP.NET Community Standup - Minimal APIs

Posted in Development