Still writing Controllers in dotNet? Give Minimal APIs a try!
Bruno Silva
Posted on September 19, 2022
If you’ve been working with .NET development for a while, you’re probably used to a lot of the quirks and idiosyncrasies from the beginning of the framework time, especially before the big transition to .NET Core. Verbose would be an understatement for the traditional Web APIs and IIS-ready apps. Wow! Remember when you needed a full-fledged Windows Server system to run your dotNet apps? Thankfully the ecosystem has evolved gigantically since then.
So your typical C# web API with Program.cs, Startup.cs and a plethora of Controllers inheriting from ControllerBase there used to be lots of naming conventions and rigid structure. Code used to look more or less like this:
using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using sandboxing.Models;
namespace sandboxing.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
public IActionResult Index()
{
return View();
}
public IActionResult Privacy()
{
return View();
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
}
Fun fact: you can type dotnet new mvc
to build a MVC style web app, if that’s still your thing. The code above is the HomeController you get from this command. You can modify your Index.cshtml to contain only a string “Hello World, I guess?”
and send it back to the browser via the controller Index()
method. When you browse to that route, the app loads the string from the controller (plus a header and footer that I really didn’t ask for).
Pretty simple, right? Well, what if could be actually simple?
With dotNet 6, Microsoft introduced the Minimal APIs paradigm, which greatly simplifies this structure and brings the C# development closer to the trendy technologies like NodeJS and Express (apparently, we’re okay with running Javascript on the backend these days).
Part of that effort has been the complete elimination of Startup.cs, which simplifies the basic boilerplate structure of Web APIs. Go ahead and type dotnet new web
.
This is what your Program.cs is going to look like:
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/", () => "Hello World!");
app.Run();
That’s it. Four lines in a single file. The app.MapGet line assigns the anonymous function “string() { return “Hello World!”; }” to the “/” route. You browse to that route; you get the return from the function. All running from a terminal application (IIS who?) that you can easily host on your server, on the cloud, on linux or Windows, wherever.
As you can see, the verbose days of dotnet apps are past. If you haven’t given C# a try in a while, this is the best time ever to get onboard and build lean apps that perform well and can be installed on any hardware. Check Microsoft's official documentation to learn more (I swear their doc is pretty good). Long live Minimal APIs!
Posted on September 19, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
September 29, 2024