Getting Started With Minimal API In .NET Core API

Introduction

Minimal APIs in .NET Core provide a lightweight and streamlined way to build HTTP APIs with minimal configuration and setup. They are particularly useful for small projects, prototypes, or simple APIs where you want to reduce ceremony and focus on writing the core functionality. Here’s how you can get started with minimal APIs in .NET Core:

Step 1: Create a Minimal API Project

  1. Open a terminal or command prompt.
  2. Navigate to the directory where you want to create your project.
  3. Run the following command to create a new minimal API project:
dotnet new web -n MinimalApiDemo

This command creates a new minimal API project named “MinimalApiDemo.”

Step 2: Define Your Minimal API

Open the Program.cs file in the project and modify it to define your minimal API:

using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

var builder = WebApplication.CreateBuilder(args);

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

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
    app.UseDeveloperExceptionPage();
}

app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "MinimalApiDemo v1"));

app.MapGet("/", () => "Hello, Minimal API!");

app.Run();

In this example, the / route responds with a “Hello, Minimal API!” message.

Step 3: Run the Minimal API

In the terminal, navigate to the project directory (MinimalApiDemo) and run the following command to start the API:

dotnet run

Your minimal API will start and be accessible at http://localhost:5000 (or https://localhost:5001 for HTTPS).

Step 4: Explore the API

Open a web browser or a tool like Postman, and navigate to http://localhost:5000 to see the “Hello, Minimal API!” response.

Optional: Add Routes and Endpoints

You can add more routes and endpoints to your minimal API by using the app.MapGet, app.MapPost, app.MapPut, and app.MapDelete methods. For example:

app.MapGet("/greet", () => "Hello, World!");

app.MapGet("/items", () => new[] { "Item 1", "Item 2", "Item 3" });

app.MapGet("/items/{id:int}", (int id) => $"Item {id}");

These lines define additional routes and their corresponding actions.

Adding Swagger Documentation (Optional)

As shown in the initial example, you can add Swagger documentation to your minimal API using the Swashbuckle.AspNetCore package. This provides a user-friendly interface for testing and exploring your API.

Summary

Minimal APIs in .NET Core provide a concise way to create simple HTTP APIs without the need for controllers or routes. They are particularly useful for small projects or when you want to quickly prototype an API. However, for more complex scenarios, you may still need to use traditional controllers and routes to handle different types of requests and actions.

Leave a Reply

Your email address will not be published. Required fields are marked *