Building Simple and Organized APIs with Minimal APIs and MapGroup() in .NET 7

Introduction

Microsoft’s .NET 7 is the latest version of their popular cross-platform development framework. One of the new features introduced in .NET 7 is the Minimal APIs, which is a new way to create lightweight, fast, and simple APIs with just a few lines of code. In this article, we’ll take a closer look at how to use Minimal APIs in .NET 7 and how to add MapGroup() to a static class.

What are Minimal APIs?

Minimal APIs are a new type of API in .NET 7 that allow developers to create APIs with minimal overhead and maximum performance. They are designed to be lightweight and easy to use, with a focus on simplicity and ease of development.

How to create a Minimal API in .NET 7?

Creating a Minimal API in .NET 7 is simple. Here’s an example of how to create a Minimal API that returns “Hello World!” when you make a GET request to the root URL:

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

In this example, we’ve used the WebApplication class to create a new Minimal API. We've then used the MapGet() method to map a GET request to the root URL ("/") and return "Hello World!".

Adding MapGroup() to a static class

MapGroup() allows you to group similar endpoints and apply shared middleware or configuration to them. To add MapGroup() to a static class, follow these steps:

1. Create a static class for your endpoints

public static class MyEndpoints
{
    public static void MapGroup(this IEndpointRouteBuilder endpoints)
    {
        endpoints.MapGet("/", Get).WithName("Get").WithOpenApi();

        endpoints.MapGet("/api/customers", GetCustomers).WithName("Customers").WithOpenApi();

        // Add more endpoints here
    }
    
    private static IResult Get()
    {
        return Results.Ok("Hello World!");
    }
    
    private static IResult GetCustomers()
    {
        return Results.Ok("List of customers");
    }
}

2. Add a MapGroup() call your endpoint configuration in the Startup class, and configure OpenAPI and Swagger services.

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();
app.MapGroup();
app.Run();

In this example, we’ve added a call to MapGroup() and passed in an empty string as the group prefix and the MapGroup() method from our MyEndpoints static class.

Now, all the endpoints defined in MyEndpoints the class will be grouped under the specified prefix, which in this case, is an empty string. You can add a different prefix to group endpoints together and use the same prefix in your middleware or configuration for all endpoints in the group.

With MapGroup(), you can create modular and organized APIs with shared middleware and configuration for groups of endpoints.

Minimal test

Conclusion

Minimal APIs in .NET 7 make it easy to create lightweight and fast APIs with just a few lines of code. Adding MapGroup() to a static class allows you to group similar endpoints and apply shared middleware or configuration to them. With these features, you can create simple and organized APIs that are easy to develop and maintain.


Similar Articles