Wisp supports defining HTTP routes directly via lightweight route handlers without requiring full controllers.
This allows for quick, minimal routing setups ideal for small apps or APIs.
You define routes by configuring them on the app builder using methods like Get
, Post
, etc.
Each route is associated with a delegate that takes an IHttpContext
and returns an IHttpResponse
or a compatible async result.
var appBuilder = new WispHostBuilder().Build();
appBuilder.ConfigureRoutes(routes =>
{
routes.Get("/hello", async ctx =>
WispHttpResponse.Ok("Hello, World!"));
routes.Post("/api/data", async ctx =>
{
var body = await ctx.Request.ReadBodyAsStringAsync();
// process data...
return WispHttpResponse.Ok("Data received");
});
// Light Routes can also have parameters in the URL
routes.Get("/hello/{name:string}", async ctx =>
{
var name = cts.Request.PathVars["id"];
return WispHttpResponse.Ok($"Hello, {name}!");
});
});
var app = appBuilder.Build();
await app.RunAsync();
Use Light Routes for:
For complex applications or when you want to separate concerns better, consider using Controllers instead.
You can mix light routes and controllers in the same application.
Wisp will dispatch requests to matching light routes first, then fallback to controller actions.