In this article, we will explore how to create a C# Minimal API project. If you want to skyrocket your C# career, check out our powerful ASP.NET full-stack web development course that also covers test-driven development.
Minimal API is a new feature in .NET 6 that simplifies the process of building APIs and makes it more accessible to developers of all levels. With its minimal configuration and ceremony, it allows developers to focus on writing code and to deliver value to their users rather than setting up infrastructure and boilerplate code. In this article, we will explore how to create a C# Minimal API project and customize it by adding endpoints and middleware.
Prerequisites
Before we begin, ensure that you have the following installed on your machine:
- .NET 6 or later SDK
- A text editor of your choice
Creating a New Project
To create a new C# Minimal API project using .NET CLI, follow the steps below:
Open your terminal or command prompt and type the following command to create a new directory for your project:
mkdir MyApi
Change into the newly created directory:
cd MyApi
Create a new C# Minimal API project using the dotnet new web
command:
dotnet new web
This command creates a new project with a default Program.cs
and a MyApi.csproj
file.
Running the Project
To run the project, use the dotnet run
command:
dotnet run
This command builds and runs the project. You should see output similar to the following:
info: Microsoft.Hosting.Lifetime[0]
Now listening on: <https://localhost:5001>
info: Microsoft.Hosting.Lifetime[0]
Now listening on: <http://localhost:5000>
Testing the API
To test the API, open a web browser and navigate to http://localhost:5000/
. You should see a Hello, World!
message.
Adding New Endpoints
To add endpoints to your C# Minimal API project, follow these steps:
Open the Program.cs
file in your project and in the Main
method, add the following code before `app.Run();`:
app.MapGet("/Hi", async context => { await context.Response.WriteAsync("Hi!"); });
This code adds a new endpoint that responds to GET
requests to the URL (/Hi
) with a Hi!
message.
- Save the file and run the project using the
dotnet run
command.
You should now be able to test your new endpoint by navigating to http://localhost:5000/Hi
in a web browser. You should see the Hi!
message returned by the endpoint.
You can add additional endpoints by calling the Map*
methods on the WebApplication
. For example, adding a POST
endpoint that returns a JSON response, you could add the following code to the Main
method:
app.MapPost("/", async context =>
{
var data = new { Message = "Hello, World from the Post endpoint!" };
await context.Response.WriteAsJsonAsync(data);
});
This code adds a new endpoint that responds to POST
requests to the root URL (/
) with a JSON response containing a Message
property with the value Hello, World!
.
If you have curl
installed on your machine, you can use curl
in your terminal to call the last endpoint.
curl -X POST http://localhost:5029/
Conclusion
In this article, we explored the steps involved in creating a C# Minimal API project using .NET CLI. and add new endpoints to it. Minimal API is an excellent addition to .NET that simplifies the process of building APIs and makes it more accessible to developers of all levels. With its minimal configuration and ceremony, it allows developers to focus on writing code and to deliver value to their users rather than setting up infrastructure and boilerplate code. With this knowledge, you can now start building your own C# Minimal API projects using .NET CLI. You can also learn more about building RESTful APIs in this article.