Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When exactly do OnResultExecuted and OnResultExecuting fire?

Tags:

I'm creating a custom ActionFilterAttribute in order to transform ViewResults and redirects into JsonResults during ajax requests. I've wired most of it through unit testing and I was for some reason assuming my transformation had to take place in OnResultExecuting, I realized this was not the case, and the correct method to override was OnActionExecuted, which is called as soon as the action method returns a result.

My question is when exactly are OnResultExecuted and OnResultExecuting being called since I've breakpoints in all four overrides and only the ones in OnActionExecuting and OnActionExecuted are firing at all.

like image 592
bevacqua Avatar asked May 03 '12 01:05

bevacqua


People also ask

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 – This method is called before a controller action is executed. OnActionExecuted – This method is called after a controller action is executed. OnResultExecuting – This method is called before a controller action result is executed.


2 Answers

Let's take the following example which performs the transformation you described:

public class MyActionFilterAttribute : ActionFilterAttribute {     public override void OnActionExecuted(ActionExecutedContext filterContext)     {         if (filterContext.HttpContext.Request.IsAjaxRequest())         {             var result = filterContext.Result as ViewResultBase;             if (result != null && result.Model != null)             {                 filterContext.Result = new JsonResult                 {                     Data = result.Model,                     JsonRequestBehavior = JsonRequestBehavior.AllowGet                 };             }         }     }      public override void OnResultExecuting(ResultExecutingContext filterContext)     {     }      public override void OnResultExecuted(ResultExecutedContext filterContext)     {     } } 

The OnResultExecuting method will be invoked immediately before the action result's ExecuteResult method runs and the OnResultExecuted method immediately after.

like image 140
Darin Dimitrov Avatar answered Nov 07 '22 00:11

Darin Dimitrov


After looking for a while and trying to understand the accepted answer I found this. I believe this shows the order of Execution to be

  1. OnActionExecuting
  2. OnActionExecuted
  3. OnResultExecuting
  4. OnResultExecuted

Check this link under listing 2. http://www.asp.net/mvc/overview/older-versions-1/controllers-and-routing/understanding-action-filters-cs

like image 41
DeadlyChambers Avatar answered Nov 06 '22 22:11

DeadlyChambers