How to Test ASP.NET Core Middleware: Unit, Integration, and Mocks

This guide covers how to effectively test ASP.NET Core middleware using unit tests, integration tests, and mocks. It includes examples of common middleware patterns, how to handle dependencies, and best practices for ensuring your middleware behaves correctly in production.

July 20, 2025 · 13 min

Dependency Inversion Principle in C#: Flexible Code with ASP.NET Core DI

Discover how the Dependency Inversion Principle makes your C# code flexible and testable. Learn to use ASP.NET Core DI to depend on abstractions, swap implementations, and build maintainable, scalable applications with real-world examples.

July 13, 2025 · 3 min

Add & Modify HTTP Headers in ASP.NET Core Middleware

This guide explains how to add and modify HTTP headers in ASP.NET Core using custom middleware. Covers dynamic header injection, security best practices, CORS configuration, and middleware pipeline ordering for robust API responses.

July 5, 2025 · 11 min

ASP.NET Core Middleware Order: Fix Pipeline Issues and Debug Execution Flow

TL;DR Middleware order in ASP.NET Core directly affects authentication, CORS, routing, and logging. Register exception handling, HTTPS redirection, and logging middleware early in the pipeline. Place CORS before authentication and routing to avoid preflight and header issues. Authentication must come before authorization and routing for secure endpoints. Use tools like MiddlewareAnalysis and custom logging middleware to debug pipeline flow. Common mistakes include routing before authentication, CORS after auth, and late exception handling. Correct middleware order prevents security holes, debugging nightmares, and production outages. The Hidden Culprit Behind Mysterious Pipeline Failures Picture this: your ASP.NET Core API works perfectly in development, but authentication randomly fails in production. CORS headers appear inconsistently. Some endpoints return 404s that should work. The logs show everything should be working correctly. ...

July 4, 2025 · 9 min

Recommended Middleware Order in ASP.NET Core for Secure, Fast, and Correct Pipelines

Discover the optimal middleware order for ASP.NET Core. See why each step matters, how to avoid common pitfalls, and use a proven pipeline template for secure, fast, and maintainable web APIs.

July 3, 2025 · Last modified: July 9, 2025 · 3 min

ASP.NET Core HTTP Logging Middleware: 13 Practical Micro Tips

TL;DR - Practical Tips for ASP.NET Core HTTP Logging Middleware Use EnableBuffering() to safely log request bodies and reset stream position after reading. Capture response bodies by swapping HttpResponse.Body with a MemoryStream and restoring after logging. Always filter or redact sensitive data before logging HTTP bodies. Set size limits and skip large or binary payloads to avoid performance issues. Filter logs by endpoint, HTTP method, or custom attributes for clarity. Built-in HttpLogging is simple; custom middleware offers full control and advanced filtering. Use structured logging and external services for production monitoring. Wrap middleware in extension methods for clean, configurable registration. Leverage PipeReader and System.IO.Pipelines for more efficient memory usage. Use Span<T> and ArrayPool<T> for zero-allocation processing of large payloads. Consider implementing request sampling in production to reduce log volume. Use a complete middleware implementation that combines all best practices. 1. How does EnableBuffering help you log request bodies in ASP.NET Core? EnableBuffering allows you to read request bodies multiple times without breaking the pipeline. ...

July 2, 2025 · Last modified: July 9, 2025 · 15 min

Implementing Request Throttling Middleware in ASP.NET Core Using MemoryCache and Per-IP Limits

Protect your ASP.NET Core APIs from abuse with custom request throttling middleware using IMemoryCache and per-IP limits. Learn to implement, configure, and monitor rate limiting for robust, high-performance APIs.

July 2, 2025 · Last modified: July 10, 2025 · 10 min

Understanding dotnet dev-certs https: Local HTTPS for .NET Development

Master local HTTPS in .NET with dotnet dev-certs https. Learn how to create, trust, and troubleshoot development certificates, avoid common pitfalls, and follow best practices for secure, productive local development.

July 1, 2025 · 6 min

Stop Repeating Yourself: Cleaner API Responses in ASP.NET Core

TL;DR Avoid repetitive response handling in ASP.NET Core controllers by using helper methods or result wrapper patterns. Helper methods reduce boilerplate for common responses like BadRequest, NotFound, and Ok. The result wrapper pattern centralizes success and error handling, making controllers cleaner and responses consistent. Keep business logic in services and HTTP response logic in controllers for better separation of concerns. Use extension methods and middleware for standardized error handling and global exception management. Consistent response patterns improve API documentation and client experience. Minimal performance impact; benefits in maintainability and clarity far outweigh the overhead. We’ve all been there. You’re building an API and you find yourself writing the same code over and over again: ...

June 26, 2025 · Last modified: July 24, 2025 · 5 min
Diagram showing HTTP request and response flow through custom logging middleware with stream capturing and rewinding techniques

Mastering Request and Response Body Logging in ASP.NET Core Middleware

TL;DR Use custom middleware to log HTTP request and response bodies in ASP.NET Core for better debugging and diagnostics. Implement stream rewinding to read request bodies without breaking downstream middleware. Always redact sensitive data and use selective logging to avoid performance and security issues. Handle large bodies by truncating logs and excluding static or health check endpoints. Consider built-in HttpLogging for simple scenarios, but use custom middleware for full control and compliance. We’ve all been there, stuck debugging an API issue for hours, wishing we could just see what’s actually coming in and going out of our application. That’s exactly what request and response body logging solves. ...

June 25, 2025 · 13 min
×