C# Interview Question #163

What is the difference between middleware and filters in ASP.NET Core?

ASP.NET Core, DI & Middleware Senior Advanced

Quick Interview Answer

Middleware operates at the HTTP pipeline level and can apply broadly to requests regardless of MVC or controller execution.

Detailed Explanation

Middleware operates at the HTTP pipeline level and can apply broadly to requests regardless of MVC or controller execution.

Filters operate within the MVC or Razor Pages action pipeline. They can run around authorization, resource processing, action execution, exception handling, and result execution.

Use middleware for application-wide HTTP concerns such as global exception handling, correlation IDs, or request logging. Use filters when behavior specifically depends on MVC action or result context.