Skip to main content

Routing

Basic routes

Each method attribute specifies the HTTP method and route:

[Get("/users")]
Task<List<UserDto>> ListUsersAsync(CancellationToken ct = default);

[Post("/users")]
Task<UserDto> CreateUserAsync([Body] CreateUserRequest body, CancellationToken ct = default);

Path parameters

Wrap the parameter name in { } in the route. The method parameter with the same name is automatically bound:

[Get("/users/{id}")]
Task<UserDto> GetUserAsync(int id, CancellationToken ct = default);

[Delete("/organizations/{orgId}/members/{userId}")]
Task RemoveMemberAsync(int orgId, int userId, CancellationToken ct = default);

Path parameters are URL-encoded with Uri.EscapeDataString before substitution.

Supported HTTP methods

AttributeHTTP verb
[Get]GET
[Post]POST
[Put]PUT
[Patch]PATCH
[Delete]DELETE

Base address

The base address is set when registering the client in DI:

services.AddIUserApi(options =>
{
options.BaseAddress = new Uri("https://api.example.com/v2");
});

The route from the attribute is appended to the base address by the underlying HttpClient.