© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•10mo ago•
39 replies
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 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;
        });
    }
    public void MapEndpoint(IEndpointRouteBuilder app)
    {
        app.MapGet("/works/{decryptedId}", async (
            DecryptedId decryptedId,
            IMediator mediator) =>
        {
            var result = await Handle(decryptedId.Id, mediator);
            return result;
        });
    }
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

✅ DateOnly.TryParse Vs DateOnly.TryParseExact use cases
C#CC# / help
11mo ago
Custom Binder Minimal API Error
C#CC# / help
8mo ago
❔ Optional parameter throwing error
C#CC# / help
4y ago
Do while with int.TryParse - how?
C#CC# / help
3y ago