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 : Router where TConfig : RouterConfig, new()
Type Parameters
| Name | Description |
|---|---|
| TRouter | The router type produced by CreateRouter(). |
| 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 |
Properties
RouterConfig
Gets the configuration object applied when CreateRouter() is called.
Declaration
public TConfig RouterConfig { get; }
Property Value
| Type | Description |
|---|---|
| TConfig |
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 |
ConfigureRouting(ConfigureBuilderDelegate<TConfig>)
Updates the router configuration object that will be used by future router instances.
Declaration
public RouterBuilder<TRouter, TConfig> ConfigureRouting(ConfigureBuilderDelegate<TConfig> updateConfig)
Parameters
| Type | Name | Description |
|---|---|---|
| ConfigureBuilderDelegate<TConfig> | updateConfig | A callback that returns the updated RouterConfig instance. |
Returns
| Type | Description |
|---|---|
| RouterBuilder<TRouter, TConfig> | The current builder. |
Examples
builder.ConfigureRouting(config => config with
{
MatchingPrecedence = MatchingPrecedence.ParameterizedFirst
});
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
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.
Examples
MyRouter router = builder.CreateRouter();