Wisp Documentation

Light Routes

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.


Defining Routes

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.

Example

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();

Route Matching

  • Routes are matched by HTTP method and exact path.
  • Routes can be registered with regex support for pattern matching.
  • The route delegate receives the context containing request and response information.

Advantages of Light Routes

  • Simplicity — No need to create classes or methods for simple endpoints.
  • Flexibility — Quickly define handlers inline or as lambdas.
  • Performance — Minimal overhead compared to full MVC pipelines.

When to Use

Use Light Routes for:

  • Simple APIs with few endpoints.
  • Prototyping or demos.
  • Static or mostly read-only routes.

For complex applications or when you want to separate concerns better, consider using Controllers instead.


Combining Light Routes and Controllers

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.