Difference between OnActionExecuted and OnResultExecuting

What is the difference between OnActionExecuted and OnResultExecuting? Do they both get fired right after each other once the action has been processed or does something happen in between these two methods. Like the initialisation of the View Engine, ...

Answers


They have a different context.

OnActionExecuted has a ActionExecutedContext in which you can view the result produced by the action. You can also see whether the action encountered an Exception and see if the exception was handled.

OnResultExecuting has a ResultExecutingContext. This method gets called just before the ActionResult instance is invoked. You can examine the result of the method and possibly cancel the execution of the result. This will usually result in a blank response with status code 200. (you can't do this in the OnActionExecuted method).


From ActionFilterAttribute.OnResultExecuting Method

Called by the ASP.NET MVC framework before the action result executes.

From ActionFilterAttribute.OnActionExecuted Method

Called by the ASP.NET MVC framework after the action method executes.


They get called after one another.

There can be no initialisation, since the ActionResult you are executing may not be rendering a View - ActionResults are free to do whatever they choose: return JSON, return a File, issue a redirect, etc.


Need Your Help