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.

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

    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