- Home
- .NET code examples
- Minimal API code example for ASP.NET Core
Minimal API code example for ASP.NET Core
We have a number of minimal API tutorials which include creating routes, adding parameters, how to return a result, filters, authentication and unit testing.
Download the code example
You'll need to fill out the code example form. We will send you an email where you can download the code example.
The code example will be in a zip file and will include all the minimal API tutorials separated by a numeric folder name.
Software
This is the software that will need to be installed onto your machine.
- Visual Studio 2022. Version 17.12.0 or above. It will work with the free community version.
- .NET 9 SDK. Version 9.0.0 or above.
Tutorials
Here are the different minimal API tutorials available:
How to use routing in Minimal APIs with one line of code
You can read more about how you create a route with one line of code or watch the video:
In Visual Studio, open up 01/RoundTheCode.MinimalApi._01.sln
from the code example zip file that you downloaded.
You can test out the different ways of registering minimal API routes by changing the toDoRoutes
variable in Program.cs
. You can change it to:
Program_cs
- This adds the routes inProgram.cs
ToDoEndpoints
- This adds the routes in theToDoEndpoints
class. This is the default.
Run the application and it will load up the Scalar OpenAPI document on https://localhost:8402/scalar/v1
.
For mapping multiple methods in the same routes and the different ways to return a response, here is what you can test:
- Hello world -
https://localhost:8402/hello-world
- Hello world with GET and HEAD HTTP requests in the same route -
https://localhost:8402/hello-world/get-head
- Hello world using a local function -
https://localhost:8402/hello-world/local
- Hello world using a class instance -
https://localhost:8402/hello-world/class-instance
- Hello world using a static method -
https://localhost:8402/hello-world/static
For the different ToDo routes, here is what you can test:
- Gets a ToDo item -
https://localhost:8402/todo/{id}
-GET
- Creates a ToDo item -
https://localhost:8402/todo
-POST
- Body:
{ "text": "{Text}" }
- Body:
- Updates a ToDo item -
https://localhost:8402/todo/{id}
-PUT
- Body:
{ "text": "{Text}" }
- Body:
- Deletes a ToDo item -
https://localhost:8402/todo/{id}
-DELETE
To test out the different ToDo routes in a group, here is what you can test:
- Gets a ToDo item -
https://localhost:8402/todo-group/{id}
-GET
- Creates a ToDo item -
https://localhost:8402/todo-group
-POST
- Body:
{ "text": "{Text}" }
- Body:
- Updates a ToDo item -
https://localhost:8402/todo-group/{id}
-PUT
- Body:
{ "text": "{Text}" }
- Body:
- Deletes a ToDo item -
https://localhost:8402/todo-group/{id}
-DELETE
Related code examples
