Class Router

Executes the route matching pipeline built by RouteScopeBuilder.

Inheritance
object
Router
Router<TDescendant, TConfig>
Inherited Members
object.GetType()
object.MemberwiseClone()
object.ToString()
object.Equals(object)
object.Equals(object, object)
object.ReferenceEquals(object, object)
object.GetHashCode()
Namespace: NanoRoute
Assembly: NanoRoute.dll
Syntax
public abstract class Router
Remarks

A router is created from a builder snapshot. Matching walks the configured route tree, attaches bound parameters, and invokes compatible handlers in order until one returns a response without delegating further.

Examples
Router router = MyRouter
    .CreateBuilder()
    .AddDefaultValueParsers()
    .AddHandler("GET", "/health/", (context, _) => Results.Ok())
    .CreateRouter();

Constructors

Router(RouteScopeBuilder, RouterConfig)

Creates a new Router instance.

Declaration
protected Router(RouteScopeBuilder routeScopeBuilder, RouterConfig config)
Parameters
Type Name Description
RouteScopeBuilder routeScopeBuilder

The builder scope whose registered routes are captured by the router.

RouterConfig config

The configuration assigned to the router instance.

Exceptions
Type Condition
ArgumentNullException

Thrown when routeScopeBuilder or config is null.

Fields

OriginalRequestName

The request property key that stores the original transport-specific request object.

Declaration
public const string OriginalRequestName = "OriginalRequest"
Field Value
Type Description
string
Examples
request.Properties[Router.OriginalRequestName] = listenerRequest;

TraceIdName

The request property key that stores the trace identifier associated with the current request.

Declaration
public const string TraceIdName = "TraceId"
Field Value
Type Description
string
Examples
request.Properties[Router.TraceIdName] = traceId;

Properties

Config

Configuration assigned to this instance.

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

Methods

Handle(HttpRequestMessage, IServiceProvider, CancellationToken)

Routes an HttpRequestMessage through the configured handler pipeline.

Declaration
protected Task<HttpResponseMessage> Handle(HttpRequestMessage request, IServiceProvider services, CancellationToken cancellation = default)
Parameters
Type Name Description
HttpRequestMessage request

The request to process.

IServiceProvider services

The service provider exposed to value parsers and handlers.

CancellationToken cancellation

A token that can cancel request processing.

Returns
Type Description
Task<HttpResponseMessage>

The HttpResponseMessage produced by the matching handlers.

Remarks

Prefix routes can participate in the same pipeline as exact routes. Consecutive / separators in the request path are treated as a single separator during matching. When several handlers match, NanoRoute evaluates compatible matches from shorter prefixes toward more specific matches and honors MatchingPrecedence when both literal and parameterized segments are available at the same depth. Once a branch is selected at a given depth, NanoRoute does not return to sibling branches later in the pipeline.

Examples
using HttpResponseMessage response = await Handle(request, services, cancellation);
Exceptions
Type Condition
HttpRequestException

Thrown when no handler matches the request path.

ArgumentNullException

Thrown when request or services is null.

ArgumentException

Thrown when the request uses an unsupported HTTP method.

OperationCanceledException

Thrown when the caller cancels the cancellation.

In this article
Back to top Generated by DocFX