Class RouterBuilder<TRouter, TConfig>

Builds a concrete Router type together with its strongly typed configuration object.

Inheritance
object
RoutingContext
RouteBuilder
RouterBuilder<TRouter, TConfig>
Inherited Members
RouteBuilder.CreatePrefix(string)
RouteBuilder.ValueParsers
RouteBuilder.Patterns
object.GetType()
object.ToString()
object.Equals(object)
object.Equals(object, object)
object.ReferenceEquals(object, object)
object.GetHashCode()
Namespace: NanoRoute
Assembly: NanoRoute.dll
Syntax
public sealed class RouterBuilder<TRouter, TConfig> : RouteBuilder where TRouter : Router where TConfig : RouterConfig, new()
Type Parameters
Name Description
TRouter

The router type produced by CreateRouter().

TConfig

The configuration type exposed by RouterConfig.

Constructors

RouterBuilder(Func<RouterBuilder<TRouter, TConfig>, TRouter>)

Creates a builder that can produce TRouter instances.

Declaration
public RouterBuilder(Func<RouterBuilder<TRouter, TConfig>, TRouter> routerFactory)
Parameters
Type Name Description
Func<RouterBuilder<TRouter, TConfig>, TRouter> routerFactory

A factory that receives this builder and returns a router backed by its current route snapshot.

Properties

RouterConfig

Gets the configuration object applied when CreateRouter() is called.

Declaration
public TConfig RouterConfig { get; }
Property Value
Type Description
TConfig

Methods

AddHandler(IEnumerable<string>, string, RequestHandlerDelegate)

Registers the same handler for multiple HTTP methods.

Declaration
public RouterBuilder<TRouter, TConfig> AddHandler(IEnumerable<string> verbs, string pattern, RequestHandlerDelegate handler)
Parameters
Type Name Description
IEnumerable<string> verbs

The HTTP methods that should use the handler.

string pattern

The route pattern to match. Literal segments are matched case-insensitively, parameter segments use registered parsers in the form {parameterName:parserName}, and a trailing / turns the pattern into a prefix match. Without a trailing slash, the pattern matches only the exact path.

RequestHandlerDelegate handler

The handler to execute when the route matches.

Returns
Type Description
RouterBuilder<TRouter, TConfig>

The current RouterBuilder<TRouter, TConfig> instance.

Examples
builder.AddHandler(
    ["GET", "POST"],
    "/api/items/{id:int}",
    (context, next) => Results.Ok(context.Parameters["id"]));
Exceptions
Type Condition
ArgumentException

Thrown when some of the verbs represent a not supported HTTP method.

InvalidOperationException

Thrown when the pattern references a value parser that has not been registered yet.

AddHandler(string, RequestHandlerDelegate)

Registers a handler for all supported HTTP methods.

Declaration
public RouterBuilder<TRouter, TConfig> AddHandler(string pattern, RequestHandlerDelegate handler)
Parameters
Type Name Description
string pattern

The route pattern to match. Literal segments are matched case-insensitively, parameter segments use registered parsers in the form {parameterName:parserName}, and a trailing / turns the pattern into a prefix match. Without a trailing slash, the pattern matches only the exact path.

RequestHandlerDelegate handler

The handler to execute when the pattern matches.

Returns
Type Description
RouterBuilder<TRouter, TConfig>

The current RouterBuilder<TRouter, TConfig> instance.

Examples
builder.AddHandler("/health", (context, next) => Results.Ok());

AddHandler(string, string, RequestHandlerDelegate)

Registers a handler for a single HTTP method.

Declaration
public RouterBuilder<TRouter, TConfig> AddHandler(string verb, string pattern, RequestHandlerDelegate handler)
Parameters
Type Name Description
string verb

The HTTP method that activates the handler.

string pattern

The route pattern to match. Literal segments are matched case-insensitively, parameter segments use registered parsers in the form {parameterName:parserName}, and a trailing / turns the pattern into a prefix match. Without a trailing slash, the pattern matches only the exact path.

RequestHandlerDelegate handler

The handler to execute. If several handlers match, calling the supplied next delegate continues the pipeline with the next compatible handler from the already selected route branch.

Returns
Type Description
RouterBuilder<TRouter, TConfig>

The current router instance.

Examples
builder.AddHandler("GET", "/files/{path:any}/", (context, next) =>
{
    string path = (string) context.Parameters["path"]!;
    return ServeFile(path);
});
Exceptions
Type Condition
ArgumentException

Thrown when verb is not a supported HTTP method.

InvalidOperationException

Thrown when the pattern references a value parser that has not been registered yet.

AddPrefix(string, Action<RouteBuilder>)

Creates a scoped child builder under the given base prefix, invokes a configuration callback, and returns this builder.

Declaration
public RouterBuilder<TRouter, TConfig> AddPrefix(string pattern, Action<RouteBuilder> configureRoutes)
Parameters
Type Name Description
string pattern

The base prefix that child routes will be registered under.

Action<RouteBuilder> configureRoutes

A callback that configures routes on the child builder.

Returns
Type Description
RouterBuilder<TRouter, TConfig>

The current builder.

Exceptions
Type Condition
ArgumentException

Thrown when pattern is not a valid route pattern or does not end with /.

InvalidOperationException

Thrown when the pattern references a value parser that has not been registered yet.

AddValueParser(string, BindArgumentsDelegate, ValueParserDelegate)

Registers a parser that can convert a route segment into a typed value and bind parser arguments once during route registration.

Declaration
public RouterBuilder<TRouter, TConfig> AddValueParser(string parserName, BindArgumentsDelegate bindArguments, ValueParserDelegate tryParseDelegate)
Parameters
Type Name Description
string parserName

The name used in route patterns such as {id:int(min=1)}.

BindArgumentsDelegate bindArguments

Converts raw parser arguments into typed values once per route-template branch.

ValueParserDelegate tryParseDelegate

The delegate that validates and parses a single path segment.

Returns
Type Description
RouterBuilder<TRouter, TConfig>

The current RouterBuilder<TRouter, TConfig> instance.

CreateRouter()

Creates a router from the builder's current routes, parser registrations, and configuration.

Declaration
public TRouter CreateRouter()
Returns
Type Description
TRouter

A new TRouter instance.

Remarks

The created router is an immutable snapshot. Later changes to the builder or its configuration do not affect routers that have already been created.

WithConfiguration(Func<TConfig, TConfig>)

Updates the router configuration object that will be used by future router instances.

Declaration
public RouterBuilder<TRouter, TConfig> WithConfiguration(Func<TConfig, TConfig> updateConfig)
Parameters
Type Name Description
Func<TConfig, TConfig> updateConfig

A callback that returns the updated RouterConfig instance.

Returns
Type Description
RouterBuilder<TRouter, TConfig>

The current builder.

Extension Methods

NanoRouteHandlerExtensions.AddHandler<TBuilder, TRequestContext>(TBuilder, IEnumerable<string>, string, Func<TRequestContext, CallNextHandlerDelegate, Task<HttpResponseMessage>>)
NanoRouteHandlerExtensions.AddHandler<TBuilder, TRequestContext>(TBuilder, IEnumerable<string>, string, Func<TRequestContext, Task<HttpResponseMessage>>)
NanoRouteHandlerExtensions.AddHandler<TBuilder, TRequestContext>(TBuilder, IEnumerable<string>, string, string, Func<TRequestContext, CallNextHandlerDelegate, Task<HttpResponseMessage>>)
NanoRouteHandlerExtensions.AddHandler<TBuilder, TRequestContext>(TBuilder, IEnumerable<string>, string, string, Func<TRequestContext, Task<HttpResponseMessage>>)
NanoRouteJsonExtensions.AddJsonBody<TBuilder>(TBuilder, IEnumerable<string>, string, JsonTypeInfo, string)
NanoRouteJsonExtensions.AddJsonBody<TBuilder>(TBuilder, IEnumerable<string>, string, Type, string)
NanoRouteJsonExtensions.AddJsonBody<TBuilder>(TBuilder, IEnumerable<string>, JsonTypeInfo, string)
NanoRouteJsonExtensions.AddJsonBody<TBuilder>(TBuilder, IEnumerable<string>, Type, string)
NanoRouteJsonExtensions.AddJsonBody<TBuilder>(TBuilder, string, string, JsonTypeInfo, string)
NanoRouteJsonExtensions.AddJsonBody<TBuilder>(TBuilder, string, string, Type, string)
NanoRouteJsonExtensions.AddJsonBody<TBuilder>(TBuilder, string, JsonTypeInfo, string)
NanoRouteJsonExtensions.AddJsonBody<TBuilder>(TBuilder, string, Type, string)
NanoRouteJsonExtensions.AddJsonBody<TBuilder>(TBuilder, JsonTypeInfo, string)
NanoRouteJsonExtensions.AddJsonBody<TBuilder>(TBuilder, Type, string)
NanoRouteJsonExtensions.AddJsonErrorDetails<TBuilder>(TBuilder, bool)
NanoRouteExceptionExtensions.AddExceptionHandler<TBuilder>(TBuilder)
NanoRouteExceptionExtensions.AddExceptionHandler<TBuilder>(TBuilder, IEnumerable<string>)
NanoRouteExceptionExtensions.AddExceptionHandler<TBuilder>(TBuilder, IEnumerable<string>, string)
NanoRouteExceptionExtensions.AddExceptionHandler<TBuilder>(TBuilder, string)
NanoRouteExceptionExtensions.AddExceptionHandler<TBuilder>(TBuilder, string, string)
NanoRouteQueryExtensions.AddQueryBindings<TBuilder>(TBuilder, IEnumerable<string>, string)
NanoRouteQueryExtensions.AddQueryBindings<TBuilder>(TBuilder, IEnumerable<string>, string, string)
NanoRouteQueryExtensions.AddQueryBindings<TBuilder>(TBuilder, string)
NanoRouteQueryExtensions.AddQueryBindings<TBuilder>(TBuilder, string, string)
NanoRouteQueryExtensions.AddQueryBindings<TBuilder>(TBuilder, string, string, string)
RouterBuilderValueParserExtensions.AddBoolParser<TBuilder>(TBuilder)
RouterBuilderValueParserExtensions.AddDefaultValueParsers<TBuilder>(TBuilder)
RouterBuilderValueParserExtensions.AddGuidParser<TBuilder>(TBuilder)
RouterBuilderValueParserExtensions.AddIntParser<TBuilder>(TBuilder)
RouterBuilderValueParserExtensions.AddStringParser<TBuilder>(TBuilder)
RouterBuilderValueParserExtensions.AddValueParser<TBuilder>(TBuilder, string, BindArgumentsDelegate, SyncValueParserDelegate)
RouterBuilderValueParserExtensions.AddValueParser<TBuilder>(TBuilder, string, BindArgumentsDelegate, ValueParserDelegate)
RouterBuilderValueParserExtensions.AddValueParser<TBuilder>(TBuilder, string, SyncValueParserDelegate)
RouterBuilderValueParserExtensions.AddValueParser<TBuilder>(TBuilder, string, ValueParserDelegate)
In this article
Back to top Generated by DocFX