Class RouterBase<TConfig>

Provides shared configuration storage and request-pipeline execution for transport-specific router adapters.

Inheritance
object
RouterBase<TConfig>
HttpListenerRouter
HttpMessageRouter
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 RouterBase<TConfig> where TConfig : RouterConfig
Type Parameters
Name Description
TConfig

The immutable router configuration type used by the adapter.

Remarks

Derive from this type when a custom transport needs to translate its native request model into an HttpRequestMessage while reusing NanoRoute's matching, value parsing, middleware, and handler pipeline. The constructor captures an immutable route snapshot, and derived adapters can call Route(HttpRequestMessage, IServiceProvider, CancellationToken) from their public entry point.

Examples
public sealed class MyRouter : RouterBase<RouterConfig>
{
    private MyRouter(RouteScopeBuilder routes, RouterConfig config)
        : base(routes, config)
    {
    }

    public async Task<HttpResponseMessage> Route(MyRequest request, IServiceProvider services, CancellationToken cancellation)
    {
        using HttpRequestMessage httpRequest = request.ToHttpRequestMessage();
        return await Route(httpRequest, services, cancellation).ConfigureAwait(false);
    }
}

Constructors

RouterBase(RouteScopeBuilder, TConfig)

Initializes a router from the current route scope and configuration.

Declaration
protected RouterBase(RouteScopeBuilder routes, TConfig config)
Parameters
Type Name Description
RouteScopeBuilder routes

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

TConfig config

The configuration assigned to the router.

Exceptions
Type Condition
ArgumentNullException

Thrown when routes or config is null.

Properties

Config

Gets the configuration assigned to this router instance.

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

Methods

Route(HttpRequestMessage, IServiceProvider, CancellationToken)

Routes a single HTTP request message and returns the produced response.

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

The request to process.

IServiceProvider services

The service provider exposed to handlers through Services.

CancellationToken cancellation

A token that can cancel request processing.

Returns
Type Description
Task<HttpResponseMessage>

The response produced by the matching handlers.

Remarks

The returned HttpResponseMessage is not disposed by the router. Callers should dispose it after reading the response body.

Exceptions
Type Condition
ArgumentNullException

Thrown when request or services is null.

InvalidOperationException

Thrown when the request uses an unsupported HTTP method.

HttpRequestException

Thrown when no handler matches the request path or a matched handler signals an HTTP failure that is not translated by middleware.

OperationCanceledException

Thrown when the caller cancels cancellation.

In this article
Back to top Generated by DocFX