C#C
C#8mo ago
Queimaduras

Parameter Binder error: implement TryParse

Hello.
Even if I'm already implementing BindAsync in my custom Binder, I get an error saying I need to implement TryParse. If I remove the parameter from the route, then it simply will ignore it:

public class DecryptedId
{
    public long Id { get; init; }

    public static ValueTask<DecryptedId?> BindAsync(HttpContext httpContext, ParameterInfo parameter)
    {
        var encoder = httpContext.RequestServices.GetRequiredService<SqidsEncoder<long>>();

        var routeId = httpContext.Request.RouteValues[parameter.Name!]?.ToString();

        if (string.IsNullOrEmpty(routeId))
            return ValueTask.FromResult<DecryptedId?>(null);

        var decryptedId = encoder.Decode(routeId).Single();

        var result = new DecryptedId
        {
            Id = decryptedId
        };

        return ValueTask.FromResult<DecryptedId?>(result);
    }
}


    public void MapEndpoint(IEndpointRouteBuilder app)
    {
        app.MapGet("/works/{decryptedId}", async (
            DecryptedId decryptedId,
            IMediator mediator) =>
        {
            var result = await Handle(decryptedId.Id, mediator);
            return result;
        });
    }
Was this page helpful?