middleware-patterns

Explain and demonstrate middleware patterns for ASP.NET Core request pipelines.

71|10|Updated Feb 11, 2026
One-click install
npx skills add https://github.com/wshaddix/dotnet-skills --skill middleware-patterns-wshaddix
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: middleware-patterns
Source: https://github.com/wshaddix/dotnet-skills/tree/main/skills/middleware-patterns
Command: npx skills add https://github.com/wshaddix/dotnet-skills --skill middleware-patterns-wshaddix

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill provides a comprehensive guide to building and managing middleware in ASP.NET Core applications, ensuring efficient and robust request processing pipelines.

Core Features & Use Cases

  • Pipeline Ordering: Understand the critical sequence of middleware registration.
  • Middleware Patterns: Learn convention-based, factory-based (IMiddleware), inline, and branching patterns.
  • Error Handling: Implement robust exception handling using built-in and custom middleware.
  • Conditional Logic: Apply middleware selectively based on request characteristics.
  • Use Case: You need to implement custom logging for all API requests, add authentication checks before specific routes, and handle exceptions gracefully. This skill shows you how to structure that pipeline.

Quick Start

Use the middleware-patterns skill to generate an example of convention-based middleware for logging request timing in an ASP.NET Core application.

Frequently Asked Questions about middleware-patterns

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

FAQPage Schema
How do I configure middleware ordering in an ASP.NET Core request pipeline?

Configuring middleware ordering in an ASP.NET Core request pipeline requires registering components sequentially in Startup or Program.cs, because the registration sequence directly dictates request and response traversal. Correct ordering ensures authentication, routing, and exception handling execute reliably.

What is the difference between convention-based and factory-based middleware in ASP.NET Core?

Convention-based middleware relies on InvokeAsync methods, while factory-based middleware uses IMiddleware to inject scoped services via dependency injection. Choosing IMiddleware provides greater flexibility for state management and testing within complex application pipelines.

How do I short-circuit the ASP.NET Core request pipeline using custom middleware?

Short-circuiting the ASP.NET Core request pipeline involves omitting the next delegate call within your custom middleware component. This immediately terminates downstream processing, preventing unnecessary route execution and returning a response directly to the client.

Does ASP.NET Core support conditional or branching middleware for specific routes?

ASP.NET Core supports conditional and branching middleware through Map and UseWhen extensions. This enables selective pipeline execution, allowing you to apply specific request manipulation or authentication checks only to requests matching defined path patterns.

What are common middleware anti-patterns when managing state in ASP.NET Core?

Common middleware anti-patterns include storing request-scoped state in singleton instances and failing to handle thread safety. Avoiding these agent gotchas ensures reliable request processing and prevents memory leaks across concurrent pipeline executions.

How do I test custom middleware in an ASP.NET Core application?

Testing custom middleware in ASP.NET Core involves verifying request and response manipulation logic using TestServer and isolated delegates. Applying these testing strategies ensures your convention-based and factory-based components behave correctly without full application startup overhead.