Minimal API (1)
Minimal API (1)
net 8
What is Minimal API?
Minimal APIs are a simple way to create HTTP APIs in .NET. They are designed to
simplify the definition and handling of HTTP requests compared to current ASP.NET Core
methods. They work well for small-scale microservices, focusing on minimizing unnecessary
code and using a straightforward programming model.
Conciseness: Minimal APIs reduce the amount of unnecessary code, allowing you to
define endpoints directly in the Program.cs file or other single files.
Performance: The reduced overhead of Minimal APIs may result in faster startup times
and lower memory usage, making them suitable for high-performance scenarios.
Simplicity: These APIs are easy to set up and manage with minimal configuration,
making them a great choice for simple applications or microservices.
Flexibility: They offer a wide range of HTTP methods and routing options, providing a
flexible approach to defining endpoints.
Built-in Dependency Injection: Minimal APIs, like other .NET APIs, use the built-in
dependency injection system to manage dependencies.
A simple example of an API that adds three decimal places and returns the result as a
response has been made by me. Another API takes three values and returns the result as a
response. The functionality of these two APIs is carried out by means of two distinct logics. In
order for us to discuss "class" and "record" in this instance. The program.cs file contains all
of the code for these APIs in this example.
First, in program.cs file adding the below code to create a builder for the web application.
Creates and configures an instance of the WebApplication class based on the settings
and services defined in the builder object.
Now defines a route for a GET request in a Minimal API application. Minimal APIs in
ASP.NET Core provide a streamlined way to define and handle HTTP requests with less
boilerplate code compared to traditional controller-based approaches
Starts the web application and begins listening for HTTP requests.
A class defining a changeable data structure with a single property result that can be set
and retrieved
Adding record
A record defining a changeable data structure with a single read-only property result. The
value is set through the constructor and cannot be changed afterward.
Key Point:
o Class is a changeable class that has constructors, extra methods, and properties. Ideal in
situations when changing an object's attributes after creation is necessary.
o Record: This data structure definition syntax is simple and unchangeable by default.
Ideal in situations when value-based equality and an immutable object are required.
Using the same calculator service as an example, I developed a repository class and
moved the logical functionality to it. In order to use the repository class and its functionality
within the program, AddSingleton must be used to register the repository's services.
FluentValidation.AspNetCore
Used for adding custom validations using RuleFor and its will return error messages if the
validation is failed.
Differences Between Minimal APIs and Controller-Based APIs
1. Code Structure:
o Minimal APIs: You can define it directly in Program.cs or a similar file using a
concise, functional style, without the need for separate controller classes.
o Minimal APIs: Setting up is easy with minimal configuration required. You can
define routes and handlers directly within the code.
3. Dependency Injection:
o Minimal APIs: Setting up dependency injection is easy and you can configure it
directly in the Program.cs file.
4. Routing:
o Minimal APIs: Routing is defined more simply using lambda expressions or
method chains.
5. Middleware:
o Minimal APIs: Middleware can be set up and applied inline, making it easier to
see the entire request handling pipeline.