Delegate ValueParserDelegate

Tries to parse a single route segment into a value that can optionally be stored in Parameters.

Namespace: NanoRoute
Assembly: NanoRoute.dll
Syntax
public delegate ValueTask<ValueParseResult> ValueParserDelegate(ValueParserContext context)
Parameters
Type Name Description
ValueParserContext context

The parser context, including the decoded route segment, request services, and the linked pipeline cancellation token.

Returns
Type Description
ValueTask<ValueParseResult>

A ValueParseResult that describes whether the segment matched and what value it produced.

Remarks

Exceptions thrown by the delegate propagate during request processing for matching routes that use the parser.

Examples
routerBuilder.AddValueParser("user", static async (ValueParserContext context) =>
{
    if (!Guid.TryParse(context.Segment.ToString(), out Guid userId))
        return ValueParseResult.False;

    object? user = await context
        .Services
        .GetRequiredService<IUserRepository>()
        .TryGetAsync(userId, context.Cancellation);

    return new ValueParseResult(user is not null, user);
});
In this article
Back to top Generated by DocFX