Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.neuro-tech.io/llms.txt

Use this file to discover all available pages before exploring further.

Neuron Http Router

Learn the TAG.Networking.HttpRouter package from setup to advanced request handling

Overview

TAG.Networking.HttpRouter is a Neuron-friendly HTTP router that can work in two styles:
  • Manual endpoint registration with RegisterGet, RegisterPost, and friends
  • Attribute-based controllers with routing, binding, and JSON handling close to the controller code
This guide focuses mainly on the attribute-based flow because that is the package’s intended high-level API and the easiest place to start. The local project and NuGet package documented here are named TAG.Networking.HttpRouter.

Install the package

dotnet add package TAG.Networking.HttpRouter
or:
<ItemGroup>
  <PackageReference Include="TAG.Networking.HttpRouter" />
</ItemGroup>

Minimal setup

Create an EndpointRouter, register it with your HTTP server, and scan an assembly for controllers.
using TAG.Networking.HttpRouter;
using Waher.Networking.HTTP;

HttpServer Server = new HttpServer(8080);
EndpointRouter Router = new EndpointRouter("/api", true);

Server.Register(Router);
Router.RegisterControllers(typeof(UsersController).Assembly);
With that setup, a controller route like /users becomes available under /api/users.

A first mental model

When a request hits the router:
  1. Matches the HTTP method and path
  2. Runs any middleware registered for that route
  3. Creates a controller instance for the request
  4. Binds route, query, and body values to method parameters
  5. Serializes the returned object to JSON

First example

[Controller]
[Route("/users")]
public sealed class UsersController : ControllerBase
{
    [HttpGet("/[userId]")]
    public Task<UserResponse> Get(string UserId)
    {
        return Task.FromResult(new UserResponse
        {
            UserId = UserId,
            DisplayName = "Ada"
        });
    }
}

public sealed class UserResponse
{
    public string UserId { get; set; } = string.Empty;
    public string DisplayName { get; set; } = string.Empty;
}
A GET request to /api/users/42 returns JSON based on the UserResponse object.

A good default order

Register middleware before calling RegisterControllers(...) if you want it to run in front of your controllers.
EndpointRouter Router = new EndpointRouter("/api", true);

Router.RegisterMiddleware("/users/*", (HttpContext Context, Action<HttpContext> Next) =>
{
    Next(Context);
    return Task.CompletedTask;
});

Router.RegisterControllers(typeof(UsersController).Assembly);