Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between OnActionExecuted and OnResultExecuting

Tags:

c#

asp.net-mvc

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, ...

like image 387
Jente Rosseel Avatar asked Nov 21 '13 15:11

Jente Rosseel


People also ask

What is OnActionExecuted?

OnActionExecuted – This method is called after a controller action is executed. OnResultExecuting – This method is called before a controller action result is executed. OnResultExecuted – This method is called after a controller action result is executed.

What is OnResultExecuting?

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.

What is difference between OnActionExecuting and OnActionExecuted in MVC?

OnActionExecuting runs before the action method. OnActionExecuted runs after the action method and can perform additional processing, such as providing extra data to the action method, inspecting the return value, or canceling execution of the action method.


2 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).

like image 122
Bart Beyers Avatar answered Sep 21 '22 13:09

Bart Beyers


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.

like image 38
Soner Gönül Avatar answered Sep 19 '22 13:09

Soner Gönül