3

Does anybody knows if the OnResultExecuted method from ActionFilterAttribute class is executed even in CATCH block?

ie:

        [CookiesActions]
        public ActionResult Login(Usuarios usuario)
[...]
            return View(new UsersViewModel(sceUsuarios.Usuario,true));
            }
            catch
            {
                return View(new UsersViewModel(new Usuarios(),true));//is OnResultExecuted fired here?
            }
[...]
Peter
  • 12,541
  • 3
  • 34
  • 39
ozsenegal
  • 4,055
  • 12
  • 49
  • 64

2 Answers2

11

In short: YES.

You can verify this with a a simple logging action filter.

public class LogAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        Log("OnActionExecuting", filterContext.RouteData);
    }

    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        Log("OnActionExecuted", filterContext.RouteData);
    }

    public override void OnResultExecuting(ResultExecutingContext filterContext)
    {
        Log("OnResultExecuting", filterContext.RouteData);
    }

    public override void OnResultExecuted(ResultExecutedContext filterContext)
    {
        Log("OnResultExecuted", filterContext.RouteData);
    }


    private void Log(string methodName, RouteData routeData)
    {
        var controllerName = routeData.Values["controller"];
        var actionName = routeData.Values["action"];
        var message = String.Format("{0} controller:{1} action:{2}", methodName, controllerName, actionName);
        Debug.WriteLine(message, "Action Filter Log");
    }

}

If you put the [Log] attribute on your method, you'll see that OnResultExecuted is written to debug even when you swallow the exception.

Peter
  • 12,541
  • 3
  • 34
  • 39
  • This answer is directly from Microsoft Docs. https://learn.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/controllers-and-routing/understanding-action-filters-cs – Hermes Trismegistus Oct 30 '18 at 15:29
  • The LogAttribute snippet may have been copied from the docs (I don't remember) but the answer is not. This code is there to demonstrate the answer to the user's question, which is that the OnResultExecuted method is called even from inside a catch block. – Peter Nov 01 '18 at 17:36
2

I don't see why it wouldn't. You are swallowing the error.

Returning out of a catch feels dirty. You could achieve the same thing without returning out of a catch:

   [CookiesActions]
    public ActionResult Login(Usuarios usuario)
    {
     [...]

           Usuarios usarios = new Usuarios();

           try
           {
             ...
             usarios = sceUsuarios.Usuario;

            }
            catch  { /*swallow error*/ }

            return View(new UsersViewModel(usarios ,true));

           [...]

    }
yoozer8
  • 7,361
  • 7
  • 58
  • 93
Chuck Conway
  • 16,287
  • 11
  • 58
  • 101
  • I cannot do that,cause i want to execute OnResultExecuted logic only if no exceptions occurs – ozsenegal Aug 25 '10 at 20:38
  • You could store a flag in the HttpContext.Items collection. In the catch you could set the flag. In the OnResultExecuted check for the flag... It's a bit kludgy... but it would work. – Chuck Conway Aug 25 '10 at 20:46