C
C#•9mo ago
Maskoe

minimal api DI magic

Why does the upper one work, but the lower one doesnt?
v1.MapGet("/lmao4", (HttpContext context) => // Executes and returns 4444.
{
Console.WriteLine("4");
return TypedResults.Ok("444");
});
v1.MapGet("/lmao3", GetAllTodos); // Executes, but doesnt return 333.

app.Run();

static async Task<IResult> GetAllTodos(HttpContext context)
{
Console.WriteLine("3");
return TypedResults.Ok("333");
}
v1.MapGet("/lmao4", (HttpContext context) => // Executes and returns 4444.
{
Console.WriteLine("4");
return TypedResults.Ok("444");
});
v1.MapGet("/lmao3", GetAllTodos); // Executes, but doesnt return 333.

app.Run();

static async Task<IResult> GetAllTodos(HttpContext context)
{
Console.WriteLine("3");
return TypedResults.Ok("333");
}
5 Replies
Maskoe
MaskoeOP•9mo ago
Adjusted my question.. pretty sure I just found a bug :thinkang: I can inject absolutely anything and do the pretty syntax anything but HttpContext IHttpContextAccessor works, [FromBody] works, random services work
Maskoe
MaskoeOP•9mo ago
holy I found the solution here https://stackoverflow.com/questions/77270839/some-minimal-api-endpoints-not-returning-values-warning-asp0016-do-not-return But now my rider ide is literaly lying to me. it works but I get an this error :200_blast:
No description
Maskoe
MaskoeOP•9mo ago
okay at least its only rider, not vs :Ok:
Pobiega
Pobiega•9mo ago
Just add a cancellation token It's best practice anyways
Maskoe
MaskoeOP•9mo ago
yea I saw that its just pretty funny, i was making a minimal example sample to show someone how easy and simple aspnetcore is compared to the go example he showed me. And immediately I ran into some weird cryptic gotcha that I needed like 2 hours to solve and im a 5 year experience all in on c# and aspnetcore andy 😹 now i hav to add "dude just add this CancellationToken thing, that you dont use, to the function signature, BUT only if you actually use the HttpContext"
Want results from more Discord servers?
Add your server