It's example i am show, who is possible does one Web Api using "Minimal Api" using Carter
-
Package Carter, more information the link below: https://github.com/CarterCommunity/Carter
-
Package FluentValidation, to validate the requests, more information the link below: https://fluentvalidation.net/
-
Top level statements (C# 9) - https://docs.microsoft.com/en-us/dotnet/csharp/fundamentals/program-structure/top-level-statements
-
Record class (C# 9) - https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/record
-
Without Startup.cs.
This is some example using Carter on .NET5.
Returning one list of Super Heros
Get("/superhero", async (request, response) =>
await response.WriteAsJsonAsync(superHero)
);
Returning one Super Hero per id
Get("superhero/{id}", async (request, response) =>{
var id = request.RouteValues.As<Guid>("id");
await response.WriteAsJsonAsync(superHero.First());
});
Returning the new Super Hero created
Post("/superhero", async (req, res) =>
{
var hero = await req.Bind<SuperHero>();
var validate = req.Validate(hero);
if (validate.IsValid)
{
res.StatusCode = 201;
await res.WriteAsJsonAsync(hero);
}
res.StatusCode = 400;
res.ContentType = "application/problem+json";
await res.WriteAsJsonAsync(validate.GetFormattedErrors());
});