Uploaded by 1176277624

Middleware vs Filters in ASP. NET Core

advertisement
2023/1/18 17:28
Middleware vs Filters ASP. NET Core - Edgeside Solutions LLC
Services


Company
Blog
Contact

Blog Post
ASP.NET CORE
April 16, 2019 Sean Leitzinger | 3 Comments
Middleware vs Filters ASP. NET Core
If you’ve been working in ASP.NET Core then you’ve probably been faced with the dilemma of
choosing between using middleware or filters. Both middleware and filters can serve similar
purposes. Choosing between them comes down to whether you need access to the MVC
context.
ASP.NET Core Middleware
The execution of middleware occurs before the MVC context becomes available in the pipeline.
That is, middleware does not have access to the ActionExecutedContext or the
ActionExecutingContext 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
https://www.edgesidesolutions.com/middleware-vs-filters-asp-net-core/
1/7
2023/1/18 17:28
Middleware vs Filters ASP. NET Core - Edgeside Solutions LLC
running a validation function or modifying values. Middleware will also run on every request
regardless of which controller or action is called.
ASP.NET Core Filters
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. Running with the example above using validation, you can run
through the full modelstate on the ActionExecutingContext. See below:
1.
2.
public class ValidationFilter : ActionFilterAttribute
{
public override Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecu
{
var modelState = context.ModelState;
3.
4.
5.
6.
7.
if (!modelState.IsValid)
8.
{
var errors = modelState.Values.SelectMany(v => v.Errors).Select(m => m.ErrorMessage).To
var stringBuilder = new StringBuilder();
errors.ForEach(m => stringBuilder.AppendLine(m));
context.Result = new BadRequestObjectResult(stringBuilder.ToString());
9.
10.
11.
12.
13.
return base.OnActionExecutionAsync(context, next);
14.
}
15.
16.
return base.OnActionExecutionAsync(context, next);
17.
}
18.
19.
}
61
Author

Sean Leitzinger
https://www.edgesidesolutions.com/middleware-vs-filters-asp-net-core/
2/7
2023/1/18 17:28
Middleware vs Filters ASP. NET Core - Edgeside Solutions LLC
CQRS with Entity Framework
Core
April 13, 2019
Testing with Entity
Framework Core
April 28, 2019
Comments (3)
AK Hasanuzzaman
 REPLY
FEBRUARY 17, 2020
Good one thanks.
Billy Larru
 REPLY
FEBRUARY 19, 2020
Excelente explicación, se agradece el aporte.
Jaco
 REPLY
APRIL 11, 2020
From .NET Core 3 you can use features to pass data from MVC to middleware and
vice/versa. So you could for example make the ModelState available for use in
middleware as well. Have a look at
https://www.edgesidesolutions.com/middleware-vs-filters-asp-net-core/

3/7
2023/1/18 17:28
Middleware vs Filters ASP. NET Core - Edgeside Solutions LLC
https://stackoverflow.com/questions/52370655/access-modelstate-in-asp-netcore-middleware/52379243 for an example.
Leave a comment
Your email address will not be published. Required fields are marked *
Comment*
Name*
Email*
POST COMMENT
Search …

Categories
.NET CORE (14)
ANGULAR (3)
ASP.NET CORE (12)

ASPECT ORIENTED PROGRAMMING (1)
https://www.edgesidesolutions.com/middleware-vs-filters-asp-net-core/
4/7
Download