Introduction
Middleware vs Filters in ASP.NET Core, both Middleware and Filters are used to handle cross-cutting concerns such as logging, authentication, authorization, and exception handling.
However, they operate at different levels of the request pipeline and serve different purposes.
Many developers confuse these two concepts, and interviewers often ask:
When should you use Middleware and when should you use Filters?
This article explains Middleware vs Filters in ASP.NET Core in depth with execution flow, use cases, code examples, and interview tips.
What is Middleware?
Definition
Middleware is a component that sits in the HTTP request pipeline and processes requests before and after they reach the MVC framework.
Middleware works globally for every request.
Request Flow
Request → Middleware → MVC → Middleware → Response
Key Characteristics
- Executes before MVC
- Works at application level
- Can short-circuit requests
- Handles cross-cutting concerns globally
Middleware Example – Custom Logging
public class RequestLoggingMiddleware
{
private readonly RequestDelegate _next;
public RequestLoggingMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext context)
{
Console.WriteLine($"Request: {context.Request.Path}");
await _next(context);
Console.WriteLine($"Response: {context.Response.StatusCode}");
}
}
Register Middleware
app.UseMiddleware<RequestLoggingMiddleware>();
Common Middleware Use Cases
✅ Global Exception Handling
✅ Authentication & Authorization
✅ Request/Response Logging
✅ Caching
✅ Rate Limiting
✅ CORS
What are Filters?
Definition
Filters are executed inside MVC, around controllers and actions.
They provide fine-grained control over specific endpoints.
Execution Scope
Middleware → MVC → Filters → Action → Filters → Result
Types of Filters in ASP.NET Core
| Filter Type | Purpose |
|---|---|
| Authorization Filter | Authorization logic |
| Resource Filter | Caching & short-circuit |
| Action Filter | Before/After action execution |
| Exception Filter | Handle exceptions in MVC |
| Result Filter | Modify action results |
Action Filter Example
public class LogActionFilter : IActionFilter
{
public void OnActionExecuting(ActionExecutingContext context)
{
Console.WriteLine("Before Action");
}
public void OnActionExecuted(ActionExecutedContext context)
{
Console.WriteLine("After Action");
}
}
Apply Filter
[ServiceFilter(typeof(LogActionFilter))]
public IActionResult Index()
{
return Ok();
}
Common Filter Use Cases
✅ Authorization per controller/action
✅ Validation
✅ Logging specific endpoints
✅ Result manipulation
✅ Exception handling in MVC
Middleware vs Filters – Key Differences
| Feature | Middleware | Filters |
|---|---|---|
| Execution Level | Application | MVC |
| Runs Before MVC | ✅ Yes | ❌ No |
| Scope | Global | Controller/Action |
| Access HttpContext | ✅ Yes | ✅ Yes |
| Short-Circuit Request | ✅ Yes | ✅ (limited) |
| Best for | Cross-cutting | MVC-specific logic |
Execution Order (Very Important for Interviews)
Actual Order
- Middleware (Incoming)
- Routing Middleware
- MVC
- Filters (Authorization → Action → Result)
- Middleware (Outgoing)
When to Use Middleware?
✔ If logic applies to every request
✔ If logic is not related to MVC
✔ If you need early request interception
Examples
- Global exception handling
- JWT authentication
- Request logging
When to Use Filters?
✔ If logic applies to specific controllers/actions
✔ If logic depends on model binding or action results
✔ If you need fine-grained control
Examples
- Role-based authorization
- Input validation
- API response formatting
Middleware vs Filters – Real Interview Question
Q: Why not use Filters instead of Middleware everywhere?
Answer:
Filters run after Middleware and only inside MVC. Middleware is required for global concerns like authentication, CORS, and exception handling before the request reaches controllers.
Best Practice (Production Rule)
Use Middleware for application-wide concerns and Filters for MVC-specific logic.
Read our detailed guides on Design Patterns & Dependency Injection in detail.

Pingback: ASP.NET Core Request Pipeline – Complete Execution Order