Every programmer’s adventure begins with the basics. Recently, I came across a very interesting topic that I would like to share with you today – minimal API. Before we get down to it, we should start with the very concept of the API mentioned above.
API (Application Programming Interface) is a set of defined rules that explain how computers or applications communicate with one another.
So far, the simplest controller in C# could have been done like this:
using System.Collections.Generic;
using System.Web.Http;
namespace MyWebAPI.Controllers
{
public class ValuesController : ApiController
{
[HttpGet]
public IEnumerable<string> Values()
{
return new string[] { "value1", "value2" };
}
[HttpGet]
public string Value(int id)
{
return "value";
}
[HttpPost]
public void SaveNewValue([FromBody] string value)
{
}
[HttpPut]
public void UpdateValue(int id, [FromBody] string value)
{
}
[HttpDelete]
public void RemoveValue(int id)
{
}
}
}
Functional API in three lines?
Minimal API allows you to build interfaces using the .NET platform in a simpler way than before. You don’t have to worry about a complicated structure and a lot of code. The simplest HTTP method, in this case GET, you can write in just three lines:
var app = WebApplication.Create(args);
app.MapGet("/", () => "Hello World!");
app.Run();
That’s all! After running the code above, a 200 OK response with “Hello World!” will be returned.
You are probably asking yourself how this is even possible. The answer is quite simple! Thanks to top-level statements – a compiler function introduced in C# 9 – you can execute a program without namespace declarations, class declarations, or even a Main(string[] args) method.
You can handle other HTTP methods in a similar way:
app.MapPost("/", () => "This is a POST");
app.MapPut("/", () => "This is a PUT");
app.MapDelete("/", () => "This is a DELETE");
Everything in one file – global directives
What about a using directive? By default, in .NET 6, ASP.NET Core will use global usings, located in a single file. Thanks to this, we will avoid having to declare them separately in each source file.
global using System;
global using System.Net.Http;
global using System.Threading.Tasks;
global using Microsoft.AspNetCore.Builder;
global using Microsoft.Extensions.Hosting;
global using Microsoft.Extensions.DependencyInjection;
Declaring a model has never been easier!
Moreover, one extra line of code is enough to allow you to work with a Person record. Positional parameters allow us to declare a model in just one line.
var app = WebApplication.Create(args);
app.MapGet("/person", () => new Person("Amy", "Scott"));
await app.RunAsync();
public record Person(string FirstName, string LastName);
Is minimal API a good solution?
If you’re new to developing APIs in ASP.NET Core, this is likely a welcome improvement. However, will it meet all the requirements on a production scale?
Minimal API is an alternative and faster way to build HTTP services, which to my mind will be a more accessible method, especially for people starting their adventure with programming.
Nonetheless, we should remember that this solution will not replace the comprehensive MVC framework.