Class NanoRouteValueParserExtensions
Provides convenience methods for registering value parsers.
Inherited Members
Namespace: NanoRoute
Assembly: NanoRoute.dll
Syntax
public static class NanoRouteValueParserExtensions
Examples
builder
.AddDefaultValueParsers()
.AddHandler("GET", "/users/{id:int}/", (context, _) => Results.Ok(context.Parameters["id"]));
Methods
AddBoolParser<TBuilder>(TBuilder)
Registers the built-in bool value parser.
Declaration
public static TBuilder AddBoolParser<TBuilder>(this TBuilder routeScopeBuilder) where TBuilder : notnull, RouteScopeBuilder
Parameters
| Type | Name | Description |
|---|---|---|
| TBuilder | routeScopeBuilder |
Returns
| Type | Description |
|---|---|
| TBuilder | The current |
Type Parameters
| Name | Description |
|---|---|
| TBuilder |
Remarks
This parser does not support any arguments.
Examples
builder
.AddBoolParser()
.AddHandler("GET", "/features/{enabled:bool}/", (context, _) => Results.Ok(context.Parameters["enabled"]));
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown when |
AddDefaultValueParsers<TBuilder>(TBuilder)
Registers the built-in value parsers for common scalar route segments.
Declaration
public static TBuilder AddDefaultValueParsers<TBuilder>(this TBuilder routeScopeBuilder) where TBuilder : notnull, RouteScopeBuilder
Parameters
| Type | Name | Description |
|---|---|---|
| TBuilder | routeScopeBuilder |
Returns
| Type | Description |
|---|---|
| TBuilder | The current |
Type Parameters
| Name | Description |
|---|---|
| TBuilder |
Remarks
This convenience method registers parsers named int, guid, bool, str, and regex.
Existing registrations with the same names are overwritten.
Examples
builder
.AddDefaultValueParsers()
.AddHandler("GET", "/users/{id:int}/", (context, next) => Results.Ok(context.Parameters["id"]));
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown when |
AddGuidParser<TBuilder>(TBuilder)
Registers the built-in guid value parser.
Declaration
public static TBuilder AddGuidParser<TBuilder>(this TBuilder routeScopeBuilder) where TBuilder : notnull, RouteScopeBuilder
Parameters
| Type | Name | Description |
|---|---|---|
| TBuilder | routeScopeBuilder |
Returns
| Type | Description |
|---|---|
| TBuilder | The current |
Type Parameters
| Name | Description |
|---|---|
| TBuilder |
Remarks
This parser does not support any arguments.
Examples
builder
.AddGuidParser()
.AddHandler("GET", "/users/{id:guid}/", (context, _) => Results.Ok(context.Parameters["id"]));
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown when |
AddIntParser<TBuilder>(TBuilder)
Registers the built-in int value parser.
Declaration
public static TBuilder AddIntParser<TBuilder>(this TBuilder routeScopeBuilder) where TBuilder : notnull, RouteScopeBuilder
Parameters
| Type | Name | Description |
|---|---|---|
| TBuilder | routeScopeBuilder |
Returns
| Type | Description |
|---|---|
| TBuilder | The current |
Type Parameters
| Name | Description |
|---|---|
| TBuilder |
Remarks
Supported arguments:
min, max.
Examples
builder
.AddIntParser()
.AddHandler("GET", "/items/{id:int(min=1)}/", (context, _) => Results.Ok(context.Parameters["id"]));
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown when |
AddRegexParser<TBuilder>(TBuilder)
Registers the built-in regex value parser.
Declaration
public static TBuilder AddRegexParser<TBuilder>(this TBuilder routeScopeBuilder) where TBuilder : notnull, RouteScopeBuilder
Parameters
| Type | Name | Description |
|---|---|---|
| TBuilder | routeScopeBuilder |
Returns
| Type | Description |
|---|---|
| TBuilder | The current |
Type Parameters
| Name | Description |
|---|---|
| TBuilder |
Remarks
Supported arguments:
pattern, timeoutMs, caseSensitive. The pattern argument is required,
timeoutMs defaults to 50, and caseSensitive defaults to false.
Examples
builder
.AddRegexParser()
.AddHandler("GET", "/tags/{slug:regex(pattern='^[a-z]+$',timeoutMs=50)}/", (context, _) => Results.Ok(context.Parameters["slug"]));
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown when |
AddStringParser<TBuilder>(TBuilder)
Registers the built-in str value parser.
Declaration
public static TBuilder AddStringParser<TBuilder>(this TBuilder routeScopeBuilder) where TBuilder : notnull, RouteScopeBuilder
Parameters
| Type | Name | Description |
|---|---|---|
| TBuilder | routeScopeBuilder |
Returns
| Type | Description |
|---|---|
| TBuilder | The current |
Type Parameters
| Name | Description |
|---|---|
| TBuilder |
Remarks
Supported arguments:
min, max.
Examples
builder
.AddStringParser()
.AddHandler("GET", "/users/{name:str(min=2)}/", (context, _) => Results.Ok(context.Parameters["name"]));
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown when |
AddValueParser<TBuilder>(TBuilder, string, BindArgumentsDelegate, SyncValueParserDelegate)
Registers a synchronous parser by adapting it to ValueParserDelegate and binding parser arguments once during route registration.
Declaration
public static TBuilder AddValueParser<TBuilder>(this TBuilder routeScopeBuilder, string parserName, BindArgumentsDelegate bindArguments, SyncValueParserDelegate tryParseDelegate) where TBuilder : notnull, RouteScopeBuilder
Parameters
| Type | Name | Description |
|---|---|---|
| TBuilder | routeScopeBuilder | |
| string | parserName | The name used in route patterns such as |
| BindArgumentsDelegate | bindArguments | Converts raw parser arguments into typed values once per route-template branch. |
| SyncValueParserDelegate | tryParseDelegate | The synchronous parser to adapt. |
Returns
| Type | Description |
|---|---|
| TBuilder | The current |
Type Parameters
| Name | Description |
|---|---|
| TBuilder |
Examples
builder.AddValueParser("str", BindStringParserArguments, TryParseStringSegment);
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown when |
AddValueParser<TBuilder>(TBuilder, string, BindArgumentsDelegate, ValueParserDelegate)
Registers an asynchronous parser and binds parser arguments once during route registration.
Declaration
public static TBuilder AddValueParser<TBuilder>(this TBuilder routeScopeBuilder, string parserName, BindArgumentsDelegate bindArguments, ValueParserDelegate tryParseDelegate) where TBuilder : notnull, RouteScopeBuilder
Parameters
| Type | Name | Description |
|---|---|---|
| TBuilder | routeScopeBuilder | |
| string | parserName | The name used in route patterns such as |
| BindArgumentsDelegate | bindArguments | Converts raw parser arguments into a parser-specific payload. |
| ValueParserDelegate | tryParseDelegate | The asynchronous parser to register. |
Returns
| Type | Description |
|---|---|
| TBuilder | The current |
Type Parameters
| Name | Description |
|---|---|
| TBuilder |
Examples
builder.AddValueParser("user", BindUserParserArguments, ParseUserAsync);
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown when |
AddValueParser<TBuilder>(TBuilder, string, SyncValueParserDelegate)
Registers a synchronous parser by adapting it to ValueParserDelegate.
Declaration
public static TBuilder AddValueParser<TBuilder>(this TBuilder routeScopeBuilder, string parserName, SyncValueParserDelegate tryParseDelegate) where TBuilder : notnull, RouteScopeBuilder
Parameters
| Type | Name | Description |
|---|---|---|
| TBuilder | routeScopeBuilder | |
| string | parserName | The name used in route patterns such as |
| SyncValueParserDelegate | tryParseDelegate | The synchronous parser to adapt. |
Returns
| Type | Description |
|---|---|
| TBuilder | The current |
Type Parameters
| Name | Description |
|---|---|
| TBuilder |
Examples
builder.AddValueParser("slug", static (ReadOnlyMemory<char> segment, object? _, out object? parsed) =>
{
parsed = segment.ToString();
return segment.Length > 0;
});
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown when |
AddValueParser<TBuilder>(TBuilder, string, ValueParserDelegate)
Registers an asynchronous parser without route-template arguments.
Declaration
public static TBuilder AddValueParser<TBuilder>(this TBuilder routeScopeBuilder, string parserName, ValueParserDelegate tryParseDelegate) where TBuilder : notnull, RouteScopeBuilder
Parameters
| Type | Name | Description |
|---|---|---|
| TBuilder | routeScopeBuilder | |
| string | parserName | The name used in route patterns such as |
| ValueParserDelegate | tryParseDelegate | The asynchronous parser to register. |
Returns
| Type | Description |
|---|---|
| TBuilder | The current |
Type Parameters
| Name | Description |
|---|---|
| TBuilder |
Examples
builder.AddValueParser("user", static async context =>
{
object? user = await FindUserAsync(context.Segment.ToString(), context.Cancellation);
return new ValueParseResult(user is not null, user);
});
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown when |