Class RouterBuilder<TRouter, TConfig>
Builds a concrete router type together with its strongly typed configuration object.
Inherited Members
Namespace: NanoRoute
Assembly: NanoRoute.dll
Syntax
public sealed class RouterBuilder<TRouter, TConfig> : RouteScopeBuilder where TRouter : RouterBase<TConfig> where TConfig : RouterConfig, new()
Type Parameters
| Name | Description |
|---|---|
| TRouter | The router type produced by CreateRouter(Action<TConfig>). |
| TConfig | The configuration type exposed by RouterConfig. |
Examples
MyRouter router = MyRouter
.CreateBuilder()
.AddDefaultValueParsers()
.AddHandler("GET", "/health/", (context, _) => Results.Ok())
.CreateRouter();
Constructors
RouterBuilder(RouterFactoryDelegate<TRouter, TConfig>)
Creates a builder that can produce TRouter instances.
Declaration
public RouterBuilder(RouterFactoryDelegate<TRouter, TConfig> routerFactory)
Parameters
| Type | Name | Description |
|---|---|---|
| RouterFactoryDelegate<TRouter, TConfig> | routerFactory | A factory that receives this builder and returns a router backed by its current route snapshot. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown when |
Methods
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 |
| RequestHandlerDelegate | handler | The handler to execute. If several handlers match, calling the supplied |
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 |
|---|---|
| ArgumentNullException | Thrown when |
| ArgumentException | Thrown when |
| ArgumentException | Thrown when |
| InvalidOperationException | Thrown when |
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 |
| 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. |
Examples
builder.AddValueParser("slug", static rawArgs => null, static context =>
ValueTask.FromResult(new ValueParseResult(context.Segment.Length > 0, context.Segment.ToString())));
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown when |
CreateRouter()
Creates a router from the builder's current routes and parser registrations with default configuration.
Declaration
public TRouter CreateRouter()
Returns
| Type | Description |
|---|---|
| TRouter | A new |
Remarks
The created router is an immutable snapshot. Later changes to the builder do not affect routers that have already been created.
Examples
MyRouter router = builder.CreateRouter();
CreateRouter(Action<TConfig>)
Creates a router from the builder's current routes and parser registrations with inline configuration.
Declaration
public TRouter CreateRouter(Action<TConfig> configureRouting)
Parameters
| Type | Name | Description |
|---|---|---|
| Action<TConfig> | configureRouting | A callback that configures the new router instance before it is created. |
Returns
| Type | Description |
|---|---|
| TRouter | A new |
Remarks
The callback receives a fresh TConfig instance for this router. Later calls to
CreateRouter(Action<TConfig>) receive their own configuration instances.
Examples
MyRouter router = builder.CreateRouter(config =>
{
config.MatchingPrecedence = MatchingPrecedence.ParameterizedFirst;
});
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown when |