asp.net-mvc asp.net 核心中间件与过滤器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42582758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:36:54  来源:igfitidea点击:

asp.net core middleware vs filters

asp.net-mvcasp.net-core

提问by Arvand

After reading about asp.net core middlware, I am confused when should I use filters and when should I use middlewares as they seem to achieve the same goal. When should middlewares be used instead of fitlers ?

在阅读了有关 asp.net core middlware 的内容后,我很困惑何时应该使用过滤器以及何时应该使用中间件,因为它们似乎实现了相同的目标。什么时候应该使用中间件而不是拟合器?

采纳答案by Arvand

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

频道 9 上有一个关于此的视频: ASP.NET Monsters #91: Middleware vs. Filters。总结一下视频:

The execution of request starts and we have a middleware, and another middlerware, 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 hereSo 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.

请求的执行开始,我们有一个中间件和另一个中间件,把它想象成“娃娃里面的俄罗斯娃娃”,最终路由中间件启动,然后请求进入 MVC 管道。 在此处输入图片说明因此,如果您不需要 MVC 的上下文(假设您关心流程和执行,例如响应某些预路由机制的标头等),则使用middlewares
但是,如果您需要 MVC 的上下文并且您想针对操作进行操作,那么请使用过滤器

回答by juunas

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

中间件在 ASP.NET Core 级别上运行,可以对进入应用程序的每个请求进行操作。

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

另一方面,MVC 过滤器仅针对到达 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.

例如,如果我想强制所有请求都必须通过 HTTPS 完成,我将不得不为此使用中间件。如果我制作了一个这样做的 MVC 过滤器,用户仍然可以通过 HTTP 请求例如静态文件。

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

但另一方面,在 MVC 控制器中记录请求持续时间的东西绝对可以是一个动作过滤器。

回答by Majid Parvin

The execution of middlewareoccurs before the MVC context becomes available in the pipeline. That is, middlewaredoes not have access to the ActionExecutingContextor the ActionExecutedContextin 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. Middlewarewill also run on every request regardless of which controller or action is called.

的执行middleware发生在 MVC 上下文在管道中可用之前。也就是说,例如,在 ActionFilter 的情况下,middleware无权访问ActionExecutingContextActionExecutedContext。您有权访问的是HttpContext,它将允许您对请求和响应执行操作。由于模型绑定尚未发生,因此使用中间件不适合运行验证功能或修改值。Middleware无论调用哪个控制器或操作,也将在每个请求上运行。

On the other hand, filterswill 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.

另一方面,filters除非您在启动时全局注册过滤器,否则只会在指定的操作和控制器上运行。由于您可以完全访问上下文,因此您还可以访问控制器和操作本身。

Source and example: dotnetcultist.com

来源和示例:dotnetcultist.com