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;
});
}
21 Replies
Queimaduras
QueimadurasOP2w ago
I assume I should just use TryParse and not lose anything? But I'm still curious about why I'm getting the error. Actually I can't use TryParse otherwise I can't get access to services
Lyrcaxis
Lyrcaxis2w ago
won't something like app.MapGet("/works/{decryptedId}", async (context) => ...) be better?
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
surf68
surf682w ago
For route, query, and header binding sources, bind custom types by adding a static TryParse method for the type.
Queimaduras
QueimadurasOP2w ago
You mean get the HttpContext directly? Without a binder?
Lyrcaxis
Lyrcaxis2w ago
yeah. It's a thing idek what a binder is
Queimaduras
QueimadurasOP2w ago
Just to decrypt the id the id is a long, I receive it as an encrypted string in the route I thought a binder would do the job
Lyrcaxis
Lyrcaxis2w ago
layers! minimal API being minimal comes first
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
Queimaduras
QueimadurasOP2w ago
I know, but TryParse don't have HttpContext
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
Queimaduras
QueimadurasOP2w ago
So I can't get the Encoder from the Services
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
Queimaduras
QueimadurasOP2w ago
If I use TryParse, the BindAsync is ignored I'll go with what @Lyrcaxis suggested
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
Queimaduras
QueimadurasOP2w ago
alr Actually, I don't think the context is even necessary I can just get the encoder directly Guess I overcomplicated stuff for no reason
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
Queimaduras
QueimadurasOP2w ago
I know, I've been using it So idk what happened for me to think of binders lol
Lyrcaxis
Lyrcaxis2w ago
ohh didn't know that.. good info 👍 although yeah hardly a need for more than HttpContext u need more u can set it up with other identifiers or cookies
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View

Did you find this page helpful?