问题描述
在阅读了 ASP.NET Core 中间件之后,我对何时应该使用过滤器以及何时应该使用中间件感到困惑,因为它们似乎实现了相同的目标.什么时候应该使用中间件而不是过滤器?
在第 9 频道上有一个关于此的视频:因此,如果您不需要 MVC 的上下文(假设您关心流程和执行,例如响应标头某些预路由机制等),则使用中间件.
但是,如果您需要 MVC 的上下文并且想要针对操作进行操作,那么请使用过滤器.
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?
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.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.
这篇关于ASP.NET Core 中间件与过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!