Class RouteBuilder
Builder responsible for route configuration.
Inherited Members
Namespace: NanoRoute
Assembly: NanoRoute.dll
Syntax
public class RouteBuilder : RoutingContext
Remarks
Route patterns support literal segments and parser-backed parameter segments such as
/users/{id:int}. Patterns must start with /, and repeated / separators such as
// are invalid. A trailing / marks the pattern as a prefix match, while patterns without a
trailing slash must match the full path exactly.
Properties
Patterns
Gets the distinct route patterns currently visible from this builder branch.
Declaration
public IEnumerable<string> Patterns { get; }
Property Value
| Type | Description |
|---|---|
| IEnumerable<string> |
Remarks
Each entry is formatted as [Verb] Pattern. Child builders list only the routes reachable from
their base path, while the root builder lists the whole configured tree.
ValueParsers
Gets the value parser registrations currently visible from this builder instance.
Declaration
public IReadOnlyDictionary<string, ValueParserRegistration> ValueParsers { get; }
Property Value
| Type | Description |
|---|---|
| IReadOnlyDictionary<string, ValueParserRegistration> |
Remarks
For child builders created with CreatePrefix(string), this dictionary reflects the inherited registrations plus any overrides added to that child scope.
Methods
AddHandler(IEnumerable<string>, string, RequestHandlerDelegate)
Registers the same handler for multiple HTTP methods.
Declaration
public RouteBuilder 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 |
|---|---|
| RouteBuilder | The current router instance. |
Examples
builder.AddHandler(
["GET", "POST"],
"/api/items/{id:int}",
(context, next) => Results.Ok(context.Parameters["id"]));
AddHandler(string, RequestHandlerDelegate)
Registers a handler for all supported HTTP methods.
Declaration
public RouteBuilder 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 |
|---|---|
| RouteBuilder | The current router instance. |
Examples
builder.AddHandler("/health", (context, next) => Results.Ok());
AddHandler(string, string, RequestHandlerDelegate)
Registers a handler for a single HTTP method.
Declaration
public RouteBuilder 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 |
|---|---|
| RouteBuilder | 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 |
AddPrefix(string, Action<RouteBuilder>)
Creates a child builder for the given prefix, invokes a configuration callback, and returns the current builder.
Declaration
public RouteBuilder AddPrefix(string pattern, Action<RouteBuilder> configureRoutes)
Parameters
| Type | Name | Description |
|---|---|---|
| string | pattern | The base prefix. It must be a valid route pattern ending in |
| Action<RouteBuilder> | configureRoutes | A callback that configures routes on the child builder. |
Returns
| Type | Description |
|---|---|
| RouteBuilder | The current builder. |
Examples
builder.AddPrefix("/api/", api => api
.AddHandler("GET", "/health", (context, _) => Results.Ok())
.AddHandler("GET", "/users", (context, _) => Results.Ok()));
Exceptions
| Type | Condition |
|---|---|
| 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 RouteBuilder 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 |
|---|---|
| RouteBuilder | The current instance. |
CreatePrefix(string)
Creates a child builder whose routes are rooted under the given prefix.
Declaration
public RouteBuilder CreatePrefix(string pattern)
Parameters
| Type | Name | Description |
|---|---|---|
| string | pattern | The base prefix. It must be a valid route pattern ending in |
Returns
| Type | Description |
|---|---|
| RouteBuilder | A child builder that shares the current route tree but has its own parser registration scope. |
Remarks
Child builders inherit the parent's registered value parsers at creation time. Additional parser registrations or overrides made on the child builder stay local to that branch.
Examples
RouteBuilder api = builder.CreatePrefix("/api/");
api.AddHandler("GET", "/health", (context, _) => Results.Ok());
Exceptions
| Type | Condition |
|---|---|
| ArgumentException | Thrown when |
| InvalidOperationException | Thrown when |