NanoRoute
NanoRoute is a small router for HttpRequestMessage pipelines with an optional HttpListener adapter.
The API documentation for this package is generated from the XML comments in the source. The links below are the intended published locations for the generated pages.
Core Types
- RouteBuilder
- Router
- RouterBuilder`2
- HttpListenerRouter
- RequestContext
- ErrorDetails
- ValueParserDelegate
- RequestHandlerDelegate
- NanoRouteHandlerExtensions
- ValueSource
- ValueSourceAttribute
Highlights
- Routes can be exact matches or prefix matches depending on whether the pattern ends with
/. - Route patterns must start with
/, and repeated/separators such as//are invalid. - Value parsers can be synchronous or asynchronous, and they can optionally bind route-template arguments such as
{id:int(min=1)}once during registration. - Parser-backed segments support optional parameter names.
{id:int}stores the parsed value inRequestContext.Parameters, while{int}only validates the segment. AddPrefix()andCreatePrefix()create scoped route subtrees without forcing you to repeat common prefixes.RouterConfigis immutable and can be replaced withWithConfiguration(config => config with { ... })before creating a router snapshot.MatchingPrecedencelets you choose whether literal or parameterized child segments are selected first.- Once a child branch has been selected for a request, NanoRoute continues only within that branch.
AddQueryBindings()uses query descriptors such as{filter:str(min=3)}&{page?:int(min=1)}and matches query keys throughUri.Querynormalization.AddHandler<TRequestContext>()can project route parameters, query bindings, services, keyed services,RequestContext, andCancellationTokeninto typed request objects.ValueSourceAttributecustomizes typed-handler property binding withContext,ServiceLocator, andSkipsources.NanoRoute.Jsonadds JSON request binding and JSON error/response helpers on top of the core pipeline.
Value Parser Syntax
{parameterName:parserName}parses a segment and stores the parsed value underparameterName.{parserName}parses a segment without storing it inRequestContext.Parameters.{parameterName:parserName(arg=value, text='hello')}also passes a case-insensitive raw argument map through the parser'sBindArgumentsDelegate.- Parser arguments support
null,trueorfalse, numbers, and single-quoted strings with\'escaping.
Use AddValueParser() to register custom parsers, or AddDefaultValueParsers() to register the built-in int, guid, bool, and str parsers.