Just curious, they sound similar. What are the differences between ExecuteCore() vs OnActionExecuting(ActionExecutingContext filterContext)?
In what situations would one be more useful over the other?
Actually, they're just different points in MVC execution pipeline.
ExecuteCore is called by MvcHandler after controller itself is instantiated. By this moment MVC doesn't even know about how controller will invoke its action. You can override standard Controller's ExecuteCore to tweak its overall execution process a little bit.
OnActionExecuting is a completely different story. It is called during action filters invocation by 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).
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. 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:
filterContext.Result = ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With