ASP.NET Tutorial: 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

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

  • 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.

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.

Before executing the project check https is selected.

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.

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.

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.

Output

URI: https://localhost:7080/getmembers

URI: https://localhost:7080/newmember

URI: https://localhost:7080/updatemember

URI: https://localhost:7080/deletemember

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

Happy Coding!

Best and Most Recommended ASP.NET Core 8.0.7 Hosting

Fortunately, there are a number of dependable and recommended web hosts available that can help you gain control of your website’s performance and improve your ASP.NET Core 9.0 web ranking. HostForLIFEASP.NET is highly recommended. In Europe, HostForLIFEASP.NET is the most popular option for first-time web hosts searching for an affordable plan.

Their standard price begins at only €3.49 per month. Customers are permitted to choose quarterly and annual plans based on their preferences. HostForLIFEASP.NET guarantees “No Hidden Fees” and an industry-leading ’30 Days Cash Back’ policy. Customers who terminate their service within the first thirty days are eligible for a full refund.

By providing reseller hosting accounts, HostForLIFEASP.NET also gives its consumers the chance to generate income. You can purchase their reseller hosting account, host an unlimited number of websites on it, and even sell some of your hosting space to others. This is one of the most effective methods for making money online. They will take care of all your customers’ hosting needs, so you do not need to fret about hosting-related matters.