- 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
How to use parameter binding for routes in Minimal APIs
You can read more about parameter binding with minimal API routes or watch the video:
In Visual Studio, open up 02/RoundTheCode.MinimalApi._02.sln
from the code example zip file that you downloaded.
Run the application and it will load up the Scalar OpenAPI document on https://localhost:8402/scalar/v1
.
Here is what you can test:
- Route parameter binding -
https://localhost:8402/todo/{id}
-GET
- Query string parameter binding -
https://localhost:8402/todo?id={id}
-GET
- Route and query string parameter binding -
https://localhost:8402/todo/from/{id}?id={id}
-GET
AsParameters
attribute -https://localhost:8402/todo/params/{id}
-GET
- Optional
page
query string - Optional
type
query string - Optional
api-key
request header
- Optional
- Wildcard route parameter -
https://localhost:8402/todo/wildcard/{*slug}
-GET
- Regex route constraint -
https://localhost:8402/todo/regex-wildcard/{*slug:regex(^[a-z0-9_-]+$)}
-GET
- Use
HttpContext
in a minimal api -https://localhost:8402/hello-world
-GET
- Route with a name -
https://localhost:8402/my-link
-GET
- Get the link with a name -
https://localhost:8402/get-my-link
-GET
- Form values -
https://localhost:8402/form-values
-POST
multipart/form-data
- Supply keys and values and see them outputted as part of the response
- Upload a singular file -
https://localhost:8402/upload-file
-POST
multipart/form-data
- Supply the
file
key with the file to upload. Use the Scalar document to do this and it will upload it to theuploads
folder in your application.
- Upload multiple files -
https://localhost:8402/upload-files
-POST
multipart/form-data
- Supply
files[*]
with the files to upload. Replace*
with the index number e.g.files[0]
. Use the Scalar document to do this and it will upload each file to theuploads
folder in your application.
TypedResults or Results for Minimal API responses?
You can read more about whether to use TypedResults or Results for minimal API responses or watch the video:
In Visual Studio, open up 03/RoundTheCode.MinimalApi._03.sln
from the code example zip file that you downloaded.
Run the application and it will load up the Scalar OpenAPI document on https://localhost:8402/scalar/v1
.
Here is what you can test. All of these endpoints use the GET
request:
- Using the
Results
type with no expectedBody
response in the OpenAPI documentation -https://localhost:8402/product/results
- Using the
TypedResults
type with an expectedProductDto
response in the OpenAPI documentation -https://localhost:8402/product/typed-results
- A JSON response -
https://localhost:8402/json
- A JSON response changing the naming policy to snake case upper -
https://localhost:8402/json-snakecaseupper
- A status code response returning a
418 I'm a Teapot
response -https://localhost:8402/status-code
- An internal server error response -
https://localhost:8402/internal-server-error
- A problem response -
https://localhost:8402/problem
- A validation problem response -
https://localhost:8402/validation-problem
- A text response -
https://localhost:8402/robots.txt
- A 301 permanent redirect to
/robots.txt
-https://localhost:8402/permanent-redirect
- A 302 temporary redirect to
/robots.txt
-https://localhost:8402/temporary-redirect
- Returns a file using
Results.File
-https://localhost:8402/file
- Returns a file using
TypedResults.File
-https://localhost:8402/file-from-stream
- Returns either an
Ok
orNotFound
response type -https://localhost:8402/different-responses/{id}
- Will return an
Ok
response if theId
is1
or less. - Will return a
NotFound
response if theId
is greater than1
.
- Will return an
Minimal API filters: Run code before the endpoint handler
You can read more about minimal API filters or watch the video:
In Visual Studio, open up 04/RoundTheCode.MinimalApi._04.sln
from the code example zip file that you downloaded.
Run the application and it will load up the Scalar OpenAPI document on https://localhost:8402/scalar/v1
.
Here is what you can test. All of these endpoints use the GET
request:
- Using filters in controllers -
https://localhost:8402/api/Product/{id}
- Will return an
Ok
response if theId
is9
or less. - Will return a
NotFound
response if theId
is greater than9
.
- Will return an
- Add an endpoint filter in a minimal API -
https://localhost:8402/api/product-minimal/{id}
- Will return an
Ok
response if theId
is9
or less. - Will return a
NotFound
response if theId
is greater than9
.
- Will return an
- Multiple endpoint filters in a minimal API -
https://localhost:8402/api/product-minimal/multiple-filters/{id}?name={name}
- Will return an
Ok
response if theId
is9
or less andName
equalsMy product
. - Will return a
NotFound
response if theId
is greater than9
orName
does not equalMy product
.
- Will return an
- Run code after the endpoint handler has been executed -
https://localhost:8402/api/product-minimal/multiple-filters-executed/{id}?name={name}
- Will return an
Ok
response if theId
is9
or less andName
equalsMy product
. - Will return a
NotFound
response if theId
is greater than9
orName
does not equalMy product
. - Timestamps will appear in the console application for both filters
- Will return an
- Move filter to its own class -
https://localhost:8402/api/product-minimal/standalone-filters/{id}
- Will return an
Ok
response if theId
is9
or less. - Will return a
NotFound
response if theId
is greater than9
.
- Will return an
Add API key authentication to an Minimal API endpoint
You can read more about how to add API key authentication to a minimal API endpoint or watch the video:
In Visual Studio, open up 05/RoundTheCode.MinimalApi._05.sln
from the code example zip file that you downloaded.
Run the application and it will load up the Scalar OpenAPI document on https://localhost:8402/scalar/v1
.
Here is what you can test. All of these endpoints use the GET
request:
- Specific route with authentication -
https://localhost:8402/hello-world
- The
api-key
request header must match what's inApi
->Key
inappsettings.Development.json
to return a 200 response. Otherwise it will return a 401 response.
- The
- Fallback policy -
https://localhost:8402/hello-world-no-authorization
- The
api-key
request header must match what's inApi
->Key
inappsettings.Development.json
to return a 200 response. Otherwise it will return a 401 response.
- The
How to add unit testing to Minimal APIs routes using xUnit
You can read more about unit testing minimal API routes, or watch the video:
In Visual Studio, open up 06/RoundTheCode.MinimalApi._06.sln
from the code example zip file that you downloaded.
There are two projects contained within the solution.
RoundTheCode.MinimalApi
- ASP.NET Core Web APIRoundTheCode.MinimalApi.XUnit
- xUnit test project that unit test classes that are inRoundTheCode.MinimalApi
In the top menu, you can go to Test and Test Explorer. This allows you to run all five tests that are in the xUnit test project. They should all pass.
Related code examples
