Simplifying Routes in .NET Core with Route Grouping: A Minimal API Approach

Streamlining Code with Elegance: Route Grouping in .NET Core Minimal APIs

Sheldon Cohen
2 min readSep 28, 2023
Streamlining Code with Elegance: A Beginner’s Guide to Route Grouping in .NET Core Minimal APIs

Minimal APIs in .NET Core allow developers to build applications with less boilerplate and more focus on the business logic. The concept of route grouping, is a technique that adheres to the Don’t Repeat Yourself (DRY) principle, ensuring our codebase is clean and maintainable.

2. Setting up the Project

Let’s set up the product using the dotnet cli

// Create a new .NET Core project
dotnet new web -o RouteGroupingApi
cd RouteGroupingApi

3. Before Route Grouping

Here is what it would look like without route grouping. Here’s a simple API representing our Products:

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

app.MapGet("/", () => "Product Api");

// Before Route Grouping
app.MapGet("/api/products", () => Results.Ok("Retrieve all products"));
app.MapGet("/api/products/{id}", (int id) => Results.Ok($"Retrieve product with ID: {id}"));
app.MapPost("", () => Results.Ok("Create a new product"));

app.Run();

This structure is straightforward, but as our application grows, maintaining individual routes can become cumbersome.

4. Introduction to Route Grouping

Route grouping is like the superhero of API organization. It allows us to group related routes together, making our API more organized and manageable. It’s especially handy when dealing with a large number of routes, ensuring our code remains neat and tidy.

5. Implementing Route Grouping

Let’s bring in route grouping to and see how we can clean this up. Below is a step-by-step guide to implementing it, with fully functional code snippets:

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

app.MapGet("/", () => "Product Api");

// Create a route group for the products API.
app.MapGroup("/api/products")
.MapProductsApi()
.WithTags("Product Api");

app.Run();

// Added to this class for simplicity, can be moved out to another class
public static class RouteBuilderExtension
{
public static RouteGroupBuilder MapProductsApi(this RouteGroupBuilder group)
{
group.MapGet("", () => Results.Ok("Retrieve all products"));
group.MapGet("{id}", (int id) => Results.Ok($"Retrieve product with ID: {id}"));
group.MapPost("", () => Results.Ok("Create a new product"));

return group;
}
}

By grouping our product-related routes under /api/products, we adhere to the DRY principle, avoiding unnecessary repetition and making our code more maintainable.

6. After Route Grouping

Voilà! Our API is now more organized, and our code is cleaner. It’s like moving from a cluttered room to an organized one — you can finally see the floor! (Or in this case, the beauty of your code.)

7. Conclusion

Route grouping in .NET Core Minimal APIs is a powerful technique for maintaining a clean and organized codebase. It not only makes our code more readable but also adheres to the DRY principle, saving us from the headache of managing individual routes in large applications. Give it a try in your code today.

8. References

--

--

Sheldon Cohen

Technology professional with 15+ years of software development