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> : 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 |
| 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 |
| InvalidOperationException | Thrown when the |
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 |
| 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 |
| 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 |
|---|---|
| ArgumentException | Thrown when |
| InvalidOperationException | Thrown when the |
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 |
| InvalidOperationException | Thrown when the |
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. |
CreateRouter()
Creates a router from the builder's current routes, parser registrations, and configuration.
Declaration
public TRouter CreateRouter()
Returns
| Type | Description |
|---|---|
| TRouter | A new |
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. |