192

After reading about ASP.NET Core middleware, I am confused about when I should use filters and when I should use middleware as they seem to achieve the same goal. When should middleware be used instead of filters?

3

5 Answers 5

163

There is a video about this on channel 9: ASP.NET Monsters #91: Middleware vs. Filters. To summarize the video:

The execution of request starts and we have a middleware, and another middleware, think of it like the "Russian dolls inside of dolls" and eventually the routing middleware kicks in and then request goes into the MVC pipline. enter image description here So if you don't require the context of MVC (let's say you're concerned about flow and execution, like responding to headers some pre-routing mechanism, etc.) then use middlewares.
But if you require the context of MVC and you want to operate against actions then use filters.

3
  • So, if I have logic I want to run on every request (Logging for instance) only some of which are MVC related, I would put that in the middleware, then have the filter handler do whatever specific logic may be required for it, then rethrow to the middleware?
    – Terry H
    Commented Nov 25, 2019 at 20:38
  • 7
    Can't you have filters in an .NET Core Web API (which is not MVC)? Commented Oct 4, 2022 at 9:13
  • @MaartenVissers your question seems confused by history. There is no such thing that I'm aware of called ".NET Core Web API". Unlike legacy ASP.NET which has MVC and "Web API 2", ASP.NET Core has only "MVC Controller with view support" Controller and "MVC Controller without view support" ControllerBase. So in the context of this answer, middleware is before the MVC pipeline, and filters participate in it.
    – Johann
    Commented Jun 4 at 22:07
114

Middleware operate on the level of ASP.NET Core and can act on every single request that comes in to the application.

MVC filters on the other hand only run for requests that come to MVC.

So for example, if I wanted to enforce that all requests must be done over HTTPS, I would have to use a middleware for that. If I made an MVC filter that did that, users could still request e.g. static files over HTTP.

But then on the other hand something that logs request durations in MVC controllers could absolutely be an action filter.

50

The execution of middleware occurs before the MVC context becomes available in the pipeline. That is, middleware does not have access to the ActionExecutingContext or the ActionExecutedContext in the case of an ActionFilter for example. What you do have access to is the HttpContext, which will allow you to perform actions on the request as well as the response. Since model binding hasn’t occurred yet, using middleware would not be suited to running a validation function or modifying values. Middleware will also run on every request regardless of which controller or action is called.

On the other hand, filters will only run on specified actions and controllers unless you register the filter globally in the startup. Since you have full access to the context you can also access the controller and action itself.

Source and example: thetechplatform.com

3
  • Your answer really help me understand clearly something. Thank you. Commented Jan 15, 2022 at 11:53
  • The attached link seems to be broken and dead Commented May 10, 2022 at 11:50
  • just replaced the link Commented May 10, 2022 at 23:52
25

Filter pipeline is similar to middleware pipeline in many ways, but they also have some differences that should be considered when deciding which approach to be used.

What are the similarities:

  • Incoming requests pass through a middleware component and outgoing responses to those requests are passing again through the same middleware when going to the client. Some filters (resource, action, and result) are also two-way, others (authorization and exception) run only once for a request, and page filters (Razor pages specific) run three times.
  • Middlewares can short-circuit a request by directly returning a response, instead of executing the entire middleware pipeline and not passing the request down to later middlewares. Filters can also short-circuit the filter pipeline by directly returning a response.
  • Middlewares are used for cross-cutting concerns (eg: logging, performance profiling or exception handling). Filters are also used for dealing with cross-cutting concerns.

What are the differences:

  • A middleware can run for all requests while filters will only run for requests that reach the EndpointMiddleware and execute an action from an API Controller or a Razor Page.
  • Filters have access to MVC components (eg: ModelState or IActionResults). Middleware works at a lower level compared to filters and is independent of MVC and Razor Pages, so it can't use any of those related components.
  • Filters are designed to be applied to a subset of requests, not to all of them. For instance, we can apply a filter on a single controller or a single Razor Page. In contrast, middlewares don't have this kind of design.

So, when trying to figure out what should use: filters or middlewares, the answer should come from the above comparison. As a TL;DR:

  • middleware is the more general concept, which operates on lower-level abstractions like HttpContext, so can be applied in a wider area. Also we should be aware of the fact that the functionality we need to implement has no MVC-specific requirements
  • filters can enable us the usage of MVC-constructs and can be used for implementing custom and specific behavior for some MVC actions. It is not so general as middleware.
1

According to my experience Middleware can be used for the entire request pipeline but Filters is only used within the Routing Middleware where we have an MVC pipeline so Middleware operates at the level of ASP.NET Core but Filters executes only when requests come to the MVC pipeline.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.