Examples

First start by registering specific middleware types or use the register all method to automatically register all used middlewares at runtime:

builder.Services.RegisterAttrMiddleware<JwtCookieMiddleware>(); // Register one
builder.Services.RegisterAllAttrMiddleware(); // Register All

You can then apply these middlewares to specific routes or controllers in your project:

[Middleware(typeof(JwtCookieMiddleware))]
public class ExampleController : ControllerBase 
{
    [HttpGet]
    public IActionResult GetRequest() => Ok(); // Uses JwtCookieMiddleware
}

[Middleware(typeof(JwtCookieMiddleware))] // Middleware is executed first
[Middleware(typeof(AnotherMiddleware))] // Middleware is executed second
public class ExampleControllerTwo : ControllerBase 
{
    [HttpGet]
    public IActionResult GetRequest() => Ok(); // Uses JwtCookieMiddleware

    [HttpGet]
    [IgnoreMiddleware]
    public IActionResult GetRequestTwo() => Ok(); // Ignores all middlewares

    [HttpGet]
    [IgnoreMiddleware(typeof(JwtCookieMiddleware))]
    public IActionResult GetRequestThree() => Ok(); // Ignores JwtCookieMiddleware
}

Minimal API's are also supported:

app.MapGet("/", () => "Hello World!").WithMiddleware(typeof(JwtCookieMiddleware));

Last updated