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.
- What is Minimal API
- Advantages of Minimal API
- Disadvantages or limitations of Minimal API
- Enhancement and Improvement in .Net 8.0
- CRUD Example
Prerequisites
- Visual Studio 2022
- 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.
- Lightweight RESTful APIs without a traditional controller
- Simplified routing
- Reduce overhead
- Improve performance
- More useful for microservices
- Accelerated Development
Now, we will discuss the disadvantages of the Minimal Api.
Disadvantages
- No building support for model binding
- No building support for validation
- No building support for filters.
- Additional learning and experience required
- No building View rending support
- Minimal APIs, being a newer approach, may feature a less developed ecosystem in comparison to the well-established ASP. NET Core MVC.
- The traditional REST API approach is more suitable for complex system
- Maintenance and readability are not suitable if we have more endpoints.
- No inbuild support for OData
- 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
- Build in Validation
- Performance improvement
- Dependency Injection
- Authorization
- Authentication
- Enhancement in Route Handling
- 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.
Step 2. Click on Next and select “.Net Core 8.0” in the addition information window.
Step 3. Click on the Create button, and the new project will be created.
Step 4. Create a Model folder and add the Employee 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”.
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.
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
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.