Search code examples
c#.net-8.0minimal-apisasp.net-core-8

Delegate 'RequestDelegate' does not take 2 arguments - ASP.NET Core 8 Minimal API


This is my Minimal API (.NET 8):

app.MapPost("check", async ([FromBody] UserClaims claims, ApplicationDbContext dbContext) =>
{
    var result = await dbContext.Users.SingleOrDefaultAsync(x => x.Phone == claims.Phone);

    if (result is null)
        return TypedResults.NotFound();

    return TypedResults.Ok();
});

enter image description here

I am getting a CS1593 error under my lambda expression.

What am I doing wrong ?

Removing the following part from my API solves the problem:

if (result is null)
    return TypedResults.NotFound();

Also, replacing TypedResults with Results solves the problem. Is there a restriction when using TypedResults?


Solution

  • From the TypedResults docs,

    To use TypedResults, the return type must be fully declared, which when asynchronous requires the Task<> wrapper.

    app.MapPost("check", async Task<Results<Ok, NotFound>> ([FromBody] UserClaims claims, ApplicationDbContext dbContext) =>
    {
        var result = await dbContext.Users.SingleOrDefaultAsync(x => x.Phone == claims.Phone);
    
        if (result is null)
            return TypedResults.NotFound();
    
        return TypedResults.Ok();
    });