middleware-patterns

Implements custom ASP.NET Core middleware patterns for request pipeline processing and cross-cutting concerns.

Updated Mar 8, 2026
One-click install
npx skills add https://github.com/AGIBuild/dotnet.CI.template --skill middleware-patterns-agibuild
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: middleware-patterns
Source: https://github.com/AGIBuild/dotnet.CI.template/tree/main/.cursor/skills/middleware-patterns
Command: npx skills add https://github.com/AGIBuild/dotnet.CI.template --skill middleware-patterns-agibuild

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Building custom middleware in ASP.NET Core is error-prone: incorrect pipeline ordering causes subtle bugs, scoped services fail in singleton middleware, and response manipulation breaks downstream components. This Skill provides tested patterns for creating, ordering, and testing middleware correctly. ## Core Features & Use Cases - Pipeline Ordering Guidance: Documents the correct registration order for exception handling, routing, CORS, authentication, and authorization, with a table of common ordering mistakes and their consequences. - Ten Middleware Patterns: Covers convention-based middleware, factory-based IMiddleware, inline app.Use/app.Run/app.Map, short-circuit logic, request/response body manipulation, IExceptionHandler (.NET 8+), conditional branching with UseWhen/MapWhen, options-based configuration, and unit testing. - Use Case: When adding API key authentication to an ASP.NET Core API, use the short-circuit pattern to reject unauthorized requests with a 401 JSON response before they reach your controllers, and place it after UseAuthorization in the pipeline. ## Quick Start Ask the agent to create a custom ASP.NET Core middleware that logs request timing and explain where to register it in the pipeline.

Frequently Asked Questions about middleware-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I create custom middleware in ASP.NET Core?▼

Create a class with a RequestDelegate constructor parameter and an InvokeAsync method, then register it with app.UseMiddleware<T>() or an extension method. For scoped dependencies, implement IMiddleware and register it with services.AddScoped<T>() first.

What is the correct middleware order in ASP.NET Core?▼

Register exception handling first, then HSTS, HTTPS redirection, static files, routing, CORS, authentication, authorization, custom middleware, and endpoint mapping last. Placing UseAuthorization before UseRouting breaks authorization because endpoint metadata is unavailable.

Convention-based middleware vs IMiddleware in ASP.NET Core?▼

Convention-based middleware is a singleton created once and receives scoped services only via InvokeAsync parameters. IMiddleware is resolved from DI per request, supports constructor injection of scoped services, but requires explicit services.AddScoped<T>() registration.

How do I read the request body in ASP.NET Core middleware?▼

Call context.Request.EnableBuffering() first, since the body stream is forward-only by default. Read the body with a StreamReader using leaveOpen: true, then reset Request.Body.Position to 0 so downstream components can read it.

How does IExceptionHandler work in .NET 8?▼

Implement IExceptionHandler with a TryHandleAsync method that returns true when the exception is handled. Register handlers with services.AddExceptionHandler<T>() in order, add ProblemDetails, and call app.UseExceptionHandler() to activate them.

Why does my middleware break the response after calling next?▼

Writing to Response.Body after next() fails if the response has already started; check context.Response.HasStarted first. To modify responses, swap Response.Body with a MemoryStream before calling next, then copy it back to the original stream.