[英]ExecuteCore() vs OnActionExecuting(ActionExecutingContext filterContext)?
Just curious, they sound similar. 只是好奇,他们听起来很相似。 What are the differences between ExecuteCore() vs OnActionExecuting(ActionExecutingContext filterContext)? ExecuteCore()和OnActionExecuting(ActionExecutingContext filterContext)之间有什么区别?
In what situations would one be more useful over the other? 在什么情况下,一个人比另一个人更有用?
Actually, they're just different points in MVC execution pipeline. 实际上,它们只是MVC执行流程中的不同点。
ExecuteCore is called by MvcHandler after controller itself is instantiated. 在实例化控制器本身之后,由MvcHandler调用ExecuteCore。 By this moment MVC doesn't even know about how controller will invoke its action. 到目前为止,MVC甚至都不知道控制器将如何调用其动作。 You can override standard Controller's ExecuteCore to tweak its overall execution process a little bit. 您可以覆盖标准Controller的ExecuteCore来稍微调整其整体执行过程。
OnActionExecuting is a completely different story. OnActionExecuting是一个完全不同的故事。 It is called during action filters invocation by ControllerActionInvoker. 它在ControllerActionInvoker的动作过滤器调用期间调用。 By that point MVC already knows that action exists, invokes it, obtains all filters (usually defined as attributes) and executes it in a given moment of overall execution pipeline (OnActionExecuting, OnActionExecuted, OnResultExecuting and so on). 到那时,MVC已经知道动作存在,调用它,获取所有过滤器(通常定义为属性)并在整个执行管道的给定时刻执行它(OnActionExecuting,OnActionExecuted,OnResultExecuting等)。
It depends on what exactly you want to achieve when deciding what extension point to use. 这取决于您在决定使用哪个扩展点时想要实现的目标。
ExecuteCore is invoked just after the controller has been initialized while OnActionExecuting happens at a later stage of the execution pipeline and is called immediately before the controller action is invoked. 在初始化控制器之后调用ExecuteCore,而OnActionExecuting在执行管道的稍后阶段发生,并在调用控制器操作之前立即调用。 In the second method you can directly manipulate the actionresult and short-circuit the execution of the action by redirecting for example to some other action: 在第二种方法中,您可以通过重定向(例如)到其他一些操作来直接操作actionresult并使操作的执行短路:
filterContext.Result = ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.