Skip to main content

.NET 8 Minimal APIs: A Step-by-Step Guide

                 .NET 8 Minimal APIs 

            In the ever-evolving world of software development, .NET 8 brings a fresh approach to building APIs with its Minimal API feature. This streamlined framework allows developers to create lightweight, high-performance APIs with minimal setup and configuration. In this blog post, we’ll explore the key features of .NET 8 Minimal APIs and walk through the steps to create your first minimal API.

Why Minimal APIs?

    Minimal APIs in .NET 8 are designed to simplify the development process by reducing boilerplate code and focusing on the essentials. Here are some benefits:

  • Simplicity: Minimal APIs require less code and configuration, making them easier to set up and maintain.
  • Performance: With fewer abstractions, Minimal APIs can offer better performance.
  • Flexibility: Ideal for microservices and small applications where a full-fledged MVC framework might be overkill.

Getting Started

Step 1: Set Up Your Development Environment

   Before diving into code, ensure you have the necessary tools:

  1. Install .NET SDK 8: Download and install the .NET SDK 8 from the official .NET website.
  2. Install Visual Studio 2022: Make sure you have Visual Studio 2022 with the ASP.NET and web development workload installed.

Step 2: Create a New Project

  1. Open Visual Studio and select Create a new project.
  2. Choose the ASP.NET Core Web API template and click Next.
  3. Name your project and solution, then click Create.
  4. In the Additional Information dialog, select .NET 8 and choose Minimal API.

ASP.NET Core Minimal API Project creation


Step 3: Define Your Model

Create a new class to represent your data model. For example, a TodoItem class:

public class TodoItem
{
    public int Id { get; set; }
    public string Name { get; set; }
    public bool IsComplete { get; set; }
}
Step 4: Create Endpoints

Open the Program.cs file and define your API endpoint

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

var todoItems = new List<TodoItem>();

app.MapGet("/todoitems", () => todoItems);

app.MapGet("/todoitems/{id}", (int id) => todoItems.FirstOrDefault(t => t.Id == id));
app.MapPost("/todoitems", (TodoItem todo) => {
    todo.Id = todoItems.Count + 1;
    todoItems.Add(todo);
    return Results.Created($"/todoitems/{todo.Id}", todo);
});

app.MapPut("/todoitems/{id}", (int id, TodoItem updatedTodo) => {
    var todo = todoItems.FirstOrDefault(t => t.Id == id);
    if (todo is null) return Results.NotFound();
    todo.Name = updatedTodo.Name;
    todo.IsComplete = updatedTodo.IsComplete;
    return Results.NoContent();
});

app.MapDelete("/todoitems/{id}", (int id) => {
    var todo = todoItems.FirstOrDefault(t => t.Id == id);
    if (todo is null) return Results.NotFound();
    todoItems.Remove(todo);
    return Results.NoContent();
});

app.Run();

Step 5: Run and Test Your API

Press F5 to run your application. You can use tools like Postman or Swagger to test your endpoints and ensure everything is working as expected.


Swagger UI

You can download the minimal api demo code from my Github Repository. 

Conclusion

    .NET 8 Minimal APIs offer a powerful and simple way to build APIs. By focusing on minimal setup and configuration, developers can quickly create high-performance APIs suitable for various applications. Whether you’re building microservices or small applications, Minimal APIs provide a flexible and efficient solution.


Comments

Popular posts from this blog

Send Meeting Invitation with C#

    So you know how to  send an email using C#  but now if you would like to attach an invitation to a meeting. We need to follow the below steps to accomplish this and it will work for the emailing apps like Outlook and Gmail.      In this code snippet I will be using an ICS(Internet Calendar Scheduling) or an iCal format for the invitation. What is ICS      An ICS (Internet Calendar Scheduling) file is a calendar file with an universal calendar format and it is used by several email providers and calendar programs, including Microsoft Outlook, Google Calendar, Notes and Apple Calendar. It enables users to publish and share calendar information on the web and over email. ICS files are often used for sending meeting requests to other users, who can import the events into their own calendars. To Send calendar invitation we need to use the System.Net.Mail namespace in .Net. And the classes required to send calendar invite are Alternat...

C# Send Email via SMTP

Simple Mail Transfer Protocol (SMTP) Simple Mail Transfer Protocol (SMTP) is a TCP/IP protocol used in sending and receiving e-mail. Most of the e-mail systems that send mail over the Internet use SMTP to send messages from one server to another. The messages can then be retrieved with an e-mail client using either POP or IMAP. SMTP Class in C#:  MailMessage class is part of the namespace System.Net.Mail and it is used to create the email messages that are sent to the SMTP Server. The delivery of the message will be taken care by the SmtpClient Class. SMTP Class Properties: Host:  Server URL for SMTP EnableSsl:  True or False. Port:  Port Number of the SMTP server Credentials:  Valid login credentials for the SMTP server (the email address and password). UseDefaultCredentials:  When we set to True then that specifies to allow authentication based on the credentials of the account used to send emails. Below are the list of few SMTP Server and Port ...