C# .NET is in development for v1. Interested in contributing or chatting with us?Get in touch!
.NET - Api.Get()
Register an API route and set a specific HTTP GET handler on that route.
This method is a convenient short version of api().route().get()
using Nitric.Sdk;
var api = Nitric.Api("main");
api.Get("/hello/:name", context => {
var name = context.Req.PathParams.get("name");
context.Res.Text($"Getting {name}!");
return context;
});
Nitric.Run();
Parameters
- Name
match
- Required
- Required
- Type
- string
- Description
The path matcher to use for the route. Matchers accept path parameters in the form of a colon prefixed string. The string provided will be used as that path parameter's name when calling middleware and handlers. See create a route with path params
- Name
...middleware
- Required
- Required
- Type
- Middleware<HttpContext> or Func<HttpContext, HttpContext>
- Description
One or more middleware functions to use as the handler for HTTP requests. Handlers can be sync or async.
Examples
Register a handler for GET requests
using Nitric.Sdk;
var api = Nitric.Api("main");
api.Get("/hello/:name", context => {
var name = context.Req.PathParams.get("name");
context.Res.Text($"Getting {name}!");
return context;
});
Nitric.Run();
Chain functions as a single method handler
When multiple functions are provided they will be called as a chain. If one succeeds, it will move on to the next. This allows middleware to be composed into more complex handlers.
using Nitric.Sdk;
var api = Nitric.Api("main");
api.Get("/hello/:userId",
(context, next) => {
var user = context.Req.PathParams["userId"];
// Validate the user identity
if (user != "1234")
{
context.Res.Text($"User {user} is unauthorised");
context.Res.Status = 403;
// Return prematurely to end the middleware chain.
return context;
}
// Call next to continue the middleware chain.
return next(context);
}, (context, next) => {
var user = context.Req.PathParams["userId"];
context.Res.Text($"Getting {user}");
return next(context);
}
);
Nitric.Run();