Introduction To Minimal API using ASP.NET Core 8

Introduction

Prior to .Net Core 6.0, REST API would be the most popular approach to creating HTTP API.

Minimal API is a new approach introduced in .Net core 6.0 to create REST API.

The aim of Minimal API is to streamline code verbosity, configuration processes, and formalities.

We will be exploring Minimal API, an exciting new feature that Microsoft has recently introduced, in this article.

In this article, we will delve into a topic that can benefit beginners, intermediates, and professionals alike.

  1. What is Minimal API
  2. Advantages of Minimal API
  3. Disadvantages or limitations of Minimal API
  4. Enhancement and Improvement in .Net 8.0
  5. CRUD Example

Prerequisites

  1. Visual Studio 2022
  2. Understanding of REST API

If you're unfamiliar with the fundamental concepts of WEB API, I suggest reading the following articles first. WebAPI

Let’s start with,

What is Minimal API?

Q. What steps should be taken to develop an HTTP API with minimal dependencies prior to .Net Core 6. 0?

Answer. The solution lies in developing an REST API that is complete with its entire structure. This means we need an entire REST API structure even for simple APIs.

To resolve this, Microsoft introduced Minimal API. Minimal APIs are designed to construct HTTP APIs with minimum dependencies. They are ideal for microservices and applications that aim to include only the essentials in terms of files, features, and dependencies within ASP. NET Core.

Advantages of Minimal API

Below are a few important advantages of the Minimal API.

  1. Lightweight RESTful APIs without a traditional controller
  2. Simplified routing
  3. Reduce overhead
  4. Improve performance
  5. More useful for microservices
  6. Accelerated Development

Now, we will discuss the disadvantages of the Minimal Api.

Disadvantages

  1. No building support for model binding
  2. No building support for validation
  3. No building support for filters.
  4. Additional learning and experience required
  5. No building View rending support
  6. Minimal APIs, being a newer approach, may feature a less developed ecosystem in comparison to the well-established ASP. NET Core MVC.
  7. The traditional REST API approach is more suitable for complex system
  8. Maintenance and readability are not suitable if we have more endpoints.
  9. No inbuild support for OData
  10. Versioning is not supported.

In the .NET 8, Microsoft has provided enhancements to the Minimal API. A few of the important improvements we will cover here,

Enhancement and Improvement in .Net 8.0

  1. Build in Validation
  2. Performance improvement
  3. Dependency Injection
  4. Authorization
  5. Authentication
  6. Enhancement in Route Handling
  7. Improvement in Swagger and OpenID integration.

Let’s create a simple Minimal API now.

CRUD Example

In this case, we will develop a basic Minimal API that handles CRUD operations.

Below are the endpoints we will create using the Mini API project.

No Task Endpoint Expected Result
1 Get All Employees /Employees Retrieved all employees
2 Get Employee by ID /Employees/{empid} Get employee requested by employee ID
3 Add new Employee /Employees Add new employee
4 Update Employee details /Employees/{empid} Update Employee details
5 Delete Employee /Employees/{empid} Delete Employee

Let's follow the below steps.

Step 1. Create an Asp.Net core Empty project.

 Asp.Net Core

Step 2. Click on Next and select “.Net Core 8.0” in the addition information window.

Addition Information

Step 3. Click on the Create button, and the new project will be created.

Create button

Step 4. Create a Model folder and add the Employee model.

Model

Employee model

public class Employee
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
}

Step 5. Get a method to retrieve all the employees.

Add the below code to the program file.

Create a sample list for this example.

List<Employee> LstEmployees = new List<Employee>()
{
    new Employee { ID = 1, Name = "Kirtesh", Address = "Vadodara" },
    new Employee { ID = 2, Name = "Sanjay", Address = "Mumbai" },
    new Employee { ID = 3, Name = "Rakesh", Address = "Surat" },
    new Employee { ID = 4, Name = "Mehul", Address = "Vadodara" },
    new Employee { ID = 4, Name = "Tushar", Address = "Vadodara" }
};

Create an Employee endpoint.

// GET call - Return all employees
app.MapGet("/Employees", () => Results.Ok(LstEmployees));

Step 6. Get the requested Employee with Employee ID.

Add the code below to the program.cs.

// GET Return Employee with emp id
app.MapGet("/Employee/{ID:int}", (int ID) =>
{
    var employee = LstEmployees.FirstOrDefault(e => e.ID == ID);
    return employee is not null ? Results.Ok(employee) : Results.NotFound();
});

Step 7. Add a new employee to the employee list.

// POST Add new employee
app.MapPost("/Employees", (Employee emp) =>
{
    emp.ID = LstEmployees.Max(e => e.ID) + 1;
    LstEmployees.Add(emp);
    return Results.Created($"New Employee {emp.ID} Added successfully", emp);
});

Step 8. Update Employee details.

// PUT Update Employee
app.MapPut("/Employee/{ID:int}", (int ID, Employee updatedEmp) =>
{
    var emp = LstEmployees.FirstOrDefault(e => e.ID == ID);
    
    if (emp is null)
        return Results.NotFound();   
    LstEmployees.Remove(emp);
    LstEmployees.Add(updatedEmp);    
    return Results.Ok(LstEmployees);
});

Step 9. Delete the requested employee from the employee list.

// DELETE
app.MapDelete("/Employee/{ID:int}", (int ID) =>
{
    var emp = LstEmployees.FirstOrDefault(e => e.ID == ID); 
    if (emp is null) 
        return Results.NotFound();
    LstEmployees.Remove(emp);
    return Results.Ok(LstEmployees);
});

Now, we will configure Swagger in the .NET Core application.

Step 10. Install “Swashbuckle.AspNetCore”.

Install

Step 11. Configure Swagger in the program.cs file.

Register services

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

Add the below middleware.

app.UseSwagger();
app.UseSwaggerUI();

Now, we will run our application and see the output.

WebApplication

Wow, we have successfully created our first simple minimal API. Let's execute the first endpoint, “GET - /Employee” to retrieve a list of all the Employees.

Output

Output

Nice. We can see a list of employees as output. Similarly, you will able to execute all the endpoints and examine the output.

I hope you enjoyed this article and learned something new.


Similar Articles