Building Minimal APIs with .NET 8 and C#

Building Minimal APIs with .NET 8 and C#

Introduction

The release of .NET 8 brings a fresh and efficient way to build APIs using the C# programming language. One of the great features is the introduction and enhancement of Minimal APIs, which allow developers to create HTTP APIs with minimal setup and boilerplate code.

This blog post aims to introduce you to Minimal APIs in .NET 8, highlighting their benefits and demonstrating how to build a simple API.

What Are Minimal APIs?

Minimal APIs in .NET 8 are a streamlined way to create lightweight HTTP services. They are designed for scenarios where you need to expose simple HTTP endpoints without the overhead of an entire MVC framework.

This approach is ideal for microservices, tiny services, or applications where you want to minimize the codebase and focus on functionality.

Setting Up the Environment

Before diving into coding, ensure the .NET 8 SDK is installed on your machine. You can download it from the official .NET website. Once installed, you can create a new project using the command line:

dotnet new web -o MyMinimalApi

Creating a Minimal API with .NET 8 and C#

Define Endpoints Directly in Program.cs:

In .NET 8, you can define HTTP endpoints directly in the Program.cs file without needing a Startup.cs file. Here’s a simple example:

var builder = WebApplication.CreateBuilder(args); 
var app = builder.Build(); 
app.MapGet("/", () => "Hello, World!"); 
app.Run();

Add Business Logic:

You can easily integrate business logic into your endpoints. For example, let’s add a simple endpoint to fetch user data:

app.MapGet("/users/{id}", (int id) => 

    // Assume UserService is a service to fetch user data 
    var user = UserService.GetUserById(id); 
    return user is not null ? Results.Ok(user) : Results.NotFound(); 
});

Run the Application: To run your application, use the following command:

dotnet run

Advantages of Using Minimal APIs

  • Simplicity: With fewer lines of code, you can set up and deploy an API, making development quicker and more efficient.
  • Performance: Minimal APIs have a lower memory footprint and faster startup times, which is crucial for microservices and cloud-native applications.
  • Flexibility: They provide a flexible way to define routes and middleware, making them a good fit for applications of various sizes and complexities.

Real-World Applications

Minimal APIs are particularly well-suited for applications like:

  • Microservices: Due to their lightweight nature.
  • Prototyping: When you need to set up an API for proof of concept quickly.
  • Serverless Applications: Where resources are constrained, and performance is critical.

Conclusion

.NET 8’s Minimal APIs bring an exciting new approach to building HTTP services with C#. They cater to the modern demand for simplicity, performance, and productivity in software development.

By embracing Minimal APIs, developers can enjoy a more streamlined and efficient workflow for creating APIs, making it easier to focus on delivering value and functionality.

Stephen

Hi, my name is Stephen Finchett. I have been a software engineer for over 30 years and worked on complex, business critical, multi-user systems for all of my career. For the last 15 years, I have been concentrating on web based solutions using the Microsoft Stack including ASP.Net, C#, TypeScript, SQL Server and running everything at scale within Kubernetes.