Overview of Minimal API

In this article you will learn about Minimal API and get the answers to the following questions.

  • What is Minimal API?
  • Difference between Minimal API and Traditional API
  • Create Minimal API step by step.
    • Types of HTTP verbs supported
    • Routing

What is Minimal API?

Minimal API is very easiest and simplest way to create HTTP APIs with Asp.Net Core technology with minimal dependencies. Minimal API is a very lightweight way to develop API. Very minimal setup and configuration settings are required to get started working on it.

Minimal API introduced in the .NET 6 version.

Features of Minimal API

  • Simplicity
  • Performance
  • Ease of Use

Difference between Minimal API and Traditional API
 

MINIMAL API TRADITIONAL API
Minimal API development without using Controller. Traditional API development with controller.
Ideal for small projects. Ideal for big projects.
No full power of MVC framework mechanism. Loaded with power of MVC framework mechanism.
Development of Minimal API on fully configuration and coding based. Development of Traditional API on convention-based.


Create Minimal API step by step

MinimalAPI

  • Point 1: Search for Asp.Net Core
  • Point 2: Select Asp.Net core empty project template
  • Point 3: Click on NEXT

Project

  • Point 1: Set your project name.
  • Point 2: Select your project folder name.
  • Point 3: Check it checkbox if you want to place the solution and project in the same directory.
  • Point 4: Click on NEXT to continue.

Additional

The above screenshot keeps as it is.

Select the latest .Net 8.0, and Check the Configure for HTTPS.

After clicking on the CREATE button, a project will be created.

Solution Explorer

Before executing the project check https is selected.

HTTP

Make sure you have selected open/run mode with HTTPS near the play button. Now press F5 to run the project.

The system will prompt you for the ASP.NET Core SSL Certificate then the Security Warning dialog boxes always select YES in both dialog boxes. This can be asked you two times repeatedly.

After successfully SSL certificate is set on the project browser will display the output.

SSL

Above is the default screen while you run the project.

Double-click on the program.cs file you can see the program.cs file is not traditionally a class or code file. .NET 6 and later the program.cs file does not have a class declaration due to the introduction of top-level statements.

.NET

Default code of program.cs file.

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
// Root or default URI of app
app.MapGet("/", () => "Hello World!");
app.Run();

You have seen in the previous output screen Hello World.

Types of HTTP verbs supported
 

MAP HTTPVERB DESCRIPTION
MapGet This is a type of HttpGet
MapPost This is a type of HttpPost
MapPut This is a type of HttpPut
MapDelete This is a type of HttpDelete

After updating the code you can see above all HttpVerb implemented.

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

// Root or default URI of app
app.MapGet("/", () => "Hello World! This is a Get");
app.MapPost("/", () => "This is a POST");
app.MapPut("/", () => "This is a PUT");
app.MapDelete("/", () => "This is a DELETE");

app.Run();

Routing

Routing is a way to respond to a client request to a specific endpoint.

Handling of routing is easy as compared to traditional controller-based routing.

Normal Routing

// Defined with routing
app.MapGet("/getmembers", () => "Get Members records");
app.MapPost("/newmember", () => "This is a new member POST request");
app.MapPut("/updatemember", () => "This is an update member PUT request");
app.MapDelete("/deletemember", () => "This is a delete member DELETE request");

Route Handlers: Route handlers are functions that execute when routing is matched.

Types of Route Handlers

  • Lambda Expression
  • Local Function
  • Instance Method

Lambda Expression: Returning text and values using lambda.

Example

app.MapGet("/getmembers", () => 
    "Get Members records");

Local Function: Local function you can create as like arrow component in REACTJS.

string GetDateTime() => DateTime.Now.ToString();
app.MapGet("/GetLocalFunction1", GetDateTime);

Full Code of program.cs

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

// Root or default URI of app
app.MapGet("/", () => "Hello World! This is a Get");
app.MapPost("/", () => "This is a POST");
app.MapPut("/", () => "This is a PUT");
app.MapDelete("/", () => "This is a DELETE");

// Defined with routing
app.MapGet("/getmembers", () => "Get Members records");
app.MapPost("/newmember", () => "This is a new member POST request");
app.MapPut("/updatemember", () => "This is an update member PUT request");
app.MapDelete("/deletemember", () => "This is a delete member DELETE request");

string GetDateTime() => DateTime.Now.ToString();
app.MapGet("/GetLocalFunction1", GetDateTime);
app.Run();

Note. To test the Minimal API you can use POSTMAN or a Browser extension base tester like- Talend API, Rest API Inspector, and API Tester.

You can visit the following article to learn more about extension-based REST API testers.

https://www.c-sharpcorner.com/article/create-custom-user-defined-web-api-get-method-and-test-in-talend-api/#:~:text=In%20this%20article%20you%20will%20learn%20about%20user%20Defined%20GET

API Tester

Output

  • URI: https://localhost:7080/getmembers
    New key
  • URI: https://localhost:7080/newmember
    API
  • URI: https://localhost:7080/updatemember
    Header
  • URI: https://localhost:7080/deletemember
    Body

In an upcoming article, you will learn how to use Entity Framework in Minimal API.

Happy Coding!