Minimal APIs are a lightweight way to build APIs with ASP.NET Core, offering a straightforward and simple approach to building web services. In this post, we'll walk through the steps to create your first Minimal API using .NET 8. This guide is perfect for those who are new to Minimal APIs or looking to explore the new features introduced in .NET 8.
What is a Minimal API?
A Minimal API in ASP.NET Core is a stripped-down version of the traditional ASP.NET Core Web API. It is designed to be simple and lightweight, using fewer files and lines of code. Minimal APIs are ideal for small applications, microservices, or scenarios where you want to quickly create an API without the overhead of a full MVC structure.
Setting Up Your Environment
Before you start, make sure you have the following tools installed:
- .NET 8 SDK: You can download it from the official .NET website.
- A Code Editor: Visual Studio Code or Visual Studio are recommended, but any text editor will work.
Step 1: Create a New .NET Project
Open your terminal or command prompt and run the following command to create a new ASP.NET Core project:
dotnet new web -n MyFirstMinimalApi
This command creates a new folder called MyFirstMinimalApi
and initializes a basic ASP.NET Core project inside it.
Step 2: Set Up the Minimal API
Navigate to the project directory:
cd MyFirstMinimalApi
Open the Program.cs
file. This file is where you'll define your Minimal API. Replace the content of Program.cs
with the following code:
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/", () => "Welcome to your first Minimal API!");
app.Run();
Explanation:
- WebApplication.CreateBuilder(args): Initializes a new web application builder.
-
app.MapGet("/"): Defines a route that listens for GET requests at the root URL (
/
). When accessed, it returns the string "Welcome to your first Minimal API!". - app.Run(): Runs the application.
Step 3: Run the API
Now, it's time to run your Minimal API. Execute the following command in your terminal:
dotnet run
Once the application is running, you should see output similar to:
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.
Open your web browser and go to http://localhost:5000
. You should see the message "Welcome to your first Minimal API!" displayed on the screen.
Step 4: Adding More Endpoints
Let's add another endpoint to your Minimal API. Modify the Program.cs
file to include a new route:
app.MapGet("/hello/{name}", (string name) => $"Hello, {name}!");
This code adds a new GET endpoint at /hello/{name}
, where {name}
is a placeholder for any name you provide in the URL.
Now, when you run your application and navigate to http://localhost:5000/hello/John
, you'll see the message "Hello, John!" on the screen.
Step 5: Adding JSON Response
Minimal APIs can also return JSON responses. Let's add an endpoint that returns a simple JSON object:
app.MapGet("/person", () => new { Name = "John Doe", Age = 30 });
Now, when you access http://localhost:5000/person
, you'll get the following JSON response:
{
"Name": "John Doe",
"Age": 30
}
Conclusion
Congratulations! You have successfully created your first Minimal API with .NET 8. Minimal APIs offer a quick and easy way to build lightweight web services without the complexity of a full MVC application. As you continue to explore .NET 8, you'll discover more powerful features that you can use to build even more robust APIs.
Happy coding!