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

2

9

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?

Carbamidine answered 6/1 at 8:6 Comment(0)
R
4

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();
});
Remy answered 6/1 at 8:21 Comment(1)
Thanks a lot. I was not aware of this.Carbamidine
C
6

While you can specify the delegate return type I would argue that switching to Results would be easier (as you discovered):

app.MapPost("check", async ([FromBody] UserClaims claims, ApplicationDbContext dbContext) =>
{
    // ...

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

    return Results.Ok();
});

Both return the same type - IResult so the compiler can infer correct return type.

Also in this particular case you can use ternary operator. If you want to keep the TypedResults it can reduce clutter a bit. For example with IResult (though arguably not much reason to do so):

app.MapPost("check", async ([FromBody] UserClaims claims, ApplicationDbContext dbContext) =>
{
    // ...

    return result is null 
        ? (IResult)TypedResults.NotFound() 
        : TypedResults.Ok();
});

Or Results<NotFound, Ok>:

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

    return result is null 
        ? (Results<NotFound, Ok>) TypedResults.NotFound() 
        : TypedResults.Ok();
});

Also check out this answer and discussions in the comments.

Carbylamine answered 6/1 at 22:2 Comment(0)
R
4

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();
});
Remy answered 6/1 at 8:21 Comment(1)
Thanks a lot. I was not aware of this.Carbamidine

© 2022 - 2024 — McMap. All rights reserved.