Delegate RequestHandlerDelegate

Represents a request handler in the router pipeline.

Namespace: NanoRoute
Assembly: NanoRoute.dll
Syntax
public delegate Task<HttpResponseMessage> RequestHandlerDelegate(RequestContext requestContext, CallNextHandlerDelegate callNext)
Parameters
Type Name Description
RequestContext requestContext

The current request context, including parsed route parameters and services.

CallNextHandlerDelegate callNext

A delegate that invokes the next compatible handler in the pipeline.

Returns
Type Description
Task<HttpResponseMessage>

The response produced by the current handler, or by a later handler when callNext is invoked.

Remarks

Handlers may signal HTTP failures by calling HttpRequestException.Throw(...). When AddJsonErrorDetails<TBuilder>(TBuilder, bool), or equivalent custom middleware is registered, those exceptions can be translated into structured error responses. Throwing other exception types is also supported, but they are treated as unexpected failures: AddExceptionHandler<TBuilder>(TBuilder) converts them into internal server error responses, while without such middleware they propagate to the caller unchanged. OperationCanceledException is left untouched so caller-driven cancellation can propagate to the transport layer or hosting code.

Examples
routerBuilder.AddHandler("GET", "/api/users/{user_id:int}/", (requestContext, callNext) =>
{
    requestContext.Parameters["User"] = LoadUser((int) requestContext.Parameters["user_id"]!);
    return callNext();
});
In this article
Back to top Generated by DocFX