Introduction To Minimal API in ASP.NET Core

What is Minimal API?

 ASP.NET Core Minimal APIs provide a streamlined approach to developing web APIs with minimal code. They prioritize conciseness, focusing on the core functionality of handling HTTP requests and responses.

Here are some key aspects of Minimal APIs:

  • Concise Syntax: They leverage top-level statements and lambda expressions to define endpoints in a very compact manner.
  • Reduced Boilerplate: Minimal APIs significantly reduce the amount of boilerplate code compared to traditional ASP.NET Core controllers.
  • Focus on Functionality: They encourage developers to prioritize the essential logic of handling requests and generating responses.
  • Flexibility: While concise, they still provide flexibility for more complex scenarios by allowing the use of middleware, dependency injection, and other features of the ASP.NET Core framework.

Let me explain with a CRUD example. (I have used .NET 9 as the target framework)

Create an ASP.NET Core Web API project with the following endpoints. In the program.cs file, add the following code.

var builder = WebApplication.CreateBuilder(args);

var app = builder.Build();

app.MapGet("/api/apple/products",()=>Results.Ok("Get all Apple products"));
app.MapGet("/api/apple/products/{id}",(int id)=>Results.Ok($"Get Apple Product by id {id}"));
app.MapPost("/api/apple/products",()=>Results.Ok("Create an Apple product"));
app.MapPut("/api/apple/products/{id}",(int id)=>Results.Ok($"Update Apple product by id {id}"));
app.MapDelete("/api/apple/products/{id}",(int id)=>Results.Ok($"Delete Apple product by id {id}"));

app.Run();

Code Explanation

1. app.MapGet("/api/apple/products", () => Results.Ok("Get all Apple products"));

  • Endpoint: /api/apple/products
  • HTTP Method: GET
  • Action: This endpoint handles HTTP GET requests to the specified URL.
  • Response: It returns a 200 OK response with the message "Get all Apple products".

2. app.MapGet("/api/apple/products/{id}", (int id) => Results.Ok($"Get Apple Product by id {id}"));

  • Endpoint: /api/apple/products/{id}
    • {id} is a route parameter that captures the ID of the product in the URL.
  • HTTP Method: GET
  • Action: This endpoint handles GET requests to the URL with a specific product ID.
  • Response: It returns a 200 OK response with a message indicating the ID of the retrieved product, e.g., "Get Apple Product by id 123".

3. app.MapPost("/api/apple/products", () => Results.Ok("Create an Apple product"));

  • Endpoint: /api/apple/products
  • HTTP Method: POST
  • Action: This endpoint handles POST requests to the URL.
  • Response: It returns a 200 OK response with the message "Create an Apple product".

4. app.MapPut("/api/apple/products/{id}", (int id) => Results.Ok($"Update Apple product by id {id}"));

  • Endpoint: /api/apple/products/{id}
  • HTTP Method: PUT
  • Action: This endpoint handles PUT requests to the URL with a specific product ID.
  • Response: It returns a 200 OK response with a message indicating the ID of the updated product, e.g., "Update Apple product by id 123".

5. app.MapDelete("/api/apple/products/{id}", (int id) => Results.Ok($"Delete Apple product by id {id}"));

  • Endpoint: /api/apple/products/{id}
  • HTTP Method: DELETE
  • Action: This endpoint handles DELETE requests to the URL with a specific product ID.
  • Response: It returns a 200 OK response with a message indicating the ID of the deleted product, e.g., "Delete Apple product by id 123".

Key Points

  • Minimal API Syntax: The code utilizes the concise syntax of Minimal APIs in ASP.NET Core.
  • Route Parameters: The {id} placeholder in the URL allows for dynamic routing based on the product ID.
  • Lambda Expressions: The endpoints are defined using lambda expressions, making the code more concise.
  • Results.Ok(): This helper method returns a 200 OK HTTP response with the specified message.

This is a basic example. In a real-world scenario, you would typically implement more complex logic within these endpoints, such as:

  • Data Access
  • Data Validation
  • Error Handling
  • Business Logic

Organizing Minimal APIs with MapGroup

MapGroup is a powerful feature in Minimal APIs that allows you to group related endpoints under a common path. This improves organization and readability, especially as your API grows in complexity.

Let me organize the above endpoints with MapGroup.

var builder = WebApplication.CreateBuilder(args);

var app = builder.Build();

var appleProductsGroup = app.MapGroup("/api/apple/products");

appleProductsGroup.MapGet("",()=>Results.Ok("Get all Apple products"));
appleProductsGroup.MapGet("{id}",(int id)=>Results.Ok($"Get Apple Product by id {id}"));
appleProductsGroup.MapPost("",()=>Results.Ok("Create an Apple product"));
appleProductsGroup.MapPut("{id}",(int id)=>Results.Ok($"Update Apple product by id {id}"));
appleProductsGroup.MapDelete("{id}",(int id)=>Results.Ok($"Delete Apple product by id {id}"));

app.Run();

var appleProductsGroup = app.MapGroup("/api/apple/products");

  • This line creates a new route group named "appleProductsGroup" that will handle requests under the path "/api/apple/products".

Key Benefits

  • Improved Code Readability: Makes the API definition more organized and easier to understand.
  • Reduced Code Duplication: Avoids repeating middleware configurations for individual routes.
  • Enhanced Maintainability: It is easier to modify or add new routes within a specific group.

Minimal APIs in ASP.NET Core provide a refreshing approach to building web APIs, emphasizing simplicity, efficiency, and a focus on core HTTP concepts.By employing MapGroup, you can establish a well-defined hierarchy within your Minimal API routes, facilitating better code organization and easier maintenance.

Happy Coding!