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?
            }
[...]

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

This answer is directly from Microsoft Docs. learn.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/…
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.
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));

           [...]

    }

2 Comments

I cannot do that,cause i want to execute OnResultExecuted logic only if no exceptions occurs
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.