Struct RequestContext
Carries request-specific data through the NanoRoute handler pipeline.
Inherited Members
Namespace: NanoRoute
Assembly: NanoRoute.dll
Syntax
public readonly struct RequestContext
Remarks
Handlers can use Parameters to share values, RemainingPath to inspect the unmatched path tail, Services to resolve dependencies, and Cancellation to observe caller-initiated cancellation. Parameters is a shared mutable dictionary for the active pipeline so handlers may overwrite values that were written earlier by other handlers.
Examples
builder.AddHandler("GET", "/users/{id:int}/", (context, _) =>
{
int id = (int) context.Parameters["id"]!;
return LoadUserResponse(id, context.Services, context.Cancellation);
});
Properties
Cancellation
Gets the cancellation token for the active request.
Declaration
public CancellationToken Cancellation { get; init; }
Property Value
| Type | Description |
|---|---|
| CancellationToken |
Examples
requestContext.Cancellation.ThrowIfCancellationRequested();
Parameters
Gets the parsed route, query, and handler-shared values for the active request.
Declaration
public required Dictionary<string, object?> Parameters { get; init; }
Property Value
| Type | Description |
|---|---|
| Dictionary<string, object> |
Examples
object? id = requestContext.Parameters["id"];
RemainingPath
Gets the request path portion that has not been consumed by the current route match.
Declaration
public required ReadOnlyMemory<char> RemainingPath { get; init; }
Property Value
| Type | Description |
|---|---|
| ReadOnlyMemory<char> |
Remarks
The value comes from System.Uri.AbsolutePath and does not include the query string. Prefix
handlers receive the unmatched tail with the leading slash before the next segment, such as
/details. Exact handlers receive an empty value when no path remains.
Examples
string tail = requestContext.RemainingPath.ToString();
Request
Gets the request being routed.
Declaration
public required HttpRequestMessage Request { get; init; }
Property Value
| Type | Description |
|---|---|
| HttpRequestMessage |
Examples
Uri? uri = requestContext.Request.RequestUri;
Services
Gets the service provider available to handlers and parsers.
Declaration
public required IServiceProvider Services { get; init; }
Property Value
| Type | Description |
|---|---|
| IServiceProvider |
Examples
IUserRepository users = requestContext.Services.GetRequiredService<IUserRepository>();