简体   繁体   English

ExecuteCore()vs OnActionExecuting(ActionExecutingContext filterContext)?

[英]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执行流程中的不同点。

  1. 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来稍微调整其整体执行过程。

  2. 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. 这取决于您在决定使用哪个扩展点时想要实现的目标。

  • Override ExecuteCore in derived Controller to tweak its common behavior (not really often the case in normal app). 在派生的Controller中覆盖ExecuteCore以调整其常见行为(在普通应用程序中通常不是这种情况)。
  • Use filters to perform some additional tasks that seem orthogonal to what acion itself is supposed to do (often this is some AOP-like logic or relates to database session/transaction management). 使用过滤器执行一些看似与acion本身应该做的正交的其他任务(通常这是一些类似AOP的逻辑或与数据库会话/事务管理有关)。

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.

相关问题 无法访问OnActionExecuting下的User.Identity(ActionExecutingContext filterContext) - Cannot access to User.Identity under OnActionExecuting(ActionExecutingContext filterContext) 重定向到链接OnActionExecuting(ActionExecutingContext) - Redirect To Link OnActionExecuting (ActionExecutingContext) AbstractValidator与OnActionExecuting - AbstractValidator Vs OnActionExecuting 如何在 IActionFilter Asp.net core 中读取 OnActionExecuting(ActionExecutingContext context) 中的请求正文 - How to read request body in OnActionExecuting(ActionExecutingContext context) in IActionFilter Asp.net core ActionFilterAttribute:何时使用OnActionExecuting与OnActionExecutingAsync? - ActionFilterAttribute: When to use OnActionExecuting vs. OnActionExecutingAsync? 使用Global.asax Begin_Request与ActionFilterAttribute OnActionExecuting - Using Global.asax Begin_Request vs ActionFilterAttribute OnActionExecuting 检索ActionExecutingContext的实例 - Retrieve Instance of ActionExecutingContext 如何使用Moq模拟ActionExecutingContext - How to mock ActionExecutingContext with moq ActionExecutingContext和HttpActionContext之间的区别 - Differences between ActionExecutingContext and HttpActionContext 确定在ExecuteCore中调用哪个控制器/动作 - Determine which controller/action is invoked in ExecuteCore
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM