Class HttpMessageRouter

Routes HttpRequestMessage instances through a NanoRoute pipeline without a transport adapter.

Inheritance
object
RouterBase<RouterConfig>
HttpMessageRouter
Inherited Members
RouterBase<RouterConfig>.Config
object.GetType()
object.ToString()
object.Equals(object)
object.Equals(object, object)
object.ReferenceEquals(object, object)
object.GetHashCode()
Namespace: NanoRoute
Assembly: NanoRoute.dll
Syntax
public sealed class HttpMessageRouter : RouterBase<RouterConfig>
Remarks

Use this router when the request is already represented as an HttpRequestMessage, such as in unit tests, HTTP-message based integrations, or custom hosting code. The caller owns disposal of the request and the returned response.

Examples
HttpMessageRouter router = HttpMessageRouter
    .CreateBuilder()
    .AddDefaultValueParsers()
    .AddHandler("GET", "/hello/{name:str}/", static (context, _) =>
        Task.FromResult(HttpResponseMessage.Json(HttpStatusCode.OK, new
        {
            message = $"Hello {context.Parameters["name"]}"
        })))
    .CreateRouter();

using HttpResponseMessage response = await router.Route(request, services, cancellationToken);

Methods

CreateBuilder()

Creates a strongly typed builder for HttpMessageRouter.

Declaration
public static RouterBuilder<HttpMessageRouter, RouterConfig> CreateBuilder()
Returns
Type Description
RouterBuilder<HttpMessageRouter, RouterConfig>

A builder that can register handlers, value parsers, and router configuration.

Examples
RouterBuilder<HttpMessageRouter, RouterConfig> builder = HttpMessageRouter.CreateBuilder();

Route(HttpRequestMessage, IServiceProvider, CancellationToken)

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

Declaration
public Task<HttpResponseMessage> Route(HttpRequestMessage request, IServiceProvider services, CancellationToken cancellation = default)
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.

Examples
using HttpRequestMessage request = new(HttpMethod.Get, "https://example.test/health");
using HttpResponseMessage response = await router.Route(request, services, cancellationToken);
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