8
votes

I need to filter only methods from all actions which have return type ActionResult from controller actions. I am getting controller name and action name from following..

string originController = filterContext.RouteData.Values["controller"].ToString();
string originAction = filterContext.RouteData.Values["action"].ToString();

but how can i filter only method which have return type ActionResult?

2
You mean that you want only actions that define ActionResult as the return type in their method signature? or you want also actions that define concrete ActionResults (like ViewResult)?haim770
no.. i want only actions which defined as ActionResult.. Can i get those from filterContext?Naveen Katakam

2 Answers

9
votes

Try this kind of code for accessing Controllers, Actions as well as

  string originController = filterContext.RouteData.Values["controller"].ToString();
  string originAction = filterContext.RouteData.Values["action"].ToString();
  string originArea = String.Empty;
   if (filterContext.RouteData.DataTokens.ContainsKey("area"))
       originArea = filterContext.RouteData.DataTokens["area"].ToString();
7
votes

Try this in your Action Filter:

var controllerActionDescriptor = filterContext.ActionDescriptor as System.Web.Mvc.ReflectedActionDescriptor;

if (controllerActionDescriptor == null ||
    controllerActionDescriptor.MethodInfo.ReturnType != typeof(ActionResult))
        {
        return;
        }

// if we got here then Action's return type is 'ActionResult'

Update:

Since you're using the OnResultExecuted method, try this:

public override void OnResultExecuted(ResultExecutedContext filterContext)
{
    string originController = filterContext.RouteData.Values["controller"].ToString();
    string originAction = filterContext.RouteData.Values["action"].ToString();

    var actionType = filterContext.Controller.GetType().GetMethod(originAction).ReturnType;

    if (actionType != typeof(ActionResult))
        return;

    // if we got here then Action's return type is 'ActionResult'
}

Update:

As per your comment, in case there is more than one Action with the same name (overloading):

public override void OnResultExecuted(ResultExecutedContext filterContext)
{
    var actionName = filterContext.RouteData.Values["action"].ToString();

    var ctlr = filterContext.Controller as Controller;
    if (ctlr == null) return;
    var invoker = ctlr.ActionInvoker as ControllerActionInvoker;
    if (invoker == null) return;

    var invokerType = invoker.GetType();
    var getCtlrDescMethod = invokerType.GetMethod("GetControllerDescriptor", BindingFlags.NonPublic | BindingFlags.Instance);
    var ctlrDesc = getCtlrDescMethod.Invoke(invoker, new object[] {ctlr.ControllerContext}) as ControllerDescriptor;

    var findActionMethod = invokerType.GetMethod("FindAction", BindingFlags.NonPublic | BindingFlags.Instance);
    var actionDesc = findActionMethod.Invoke(invoker, new object[] { ctlr.ControllerContext, ctlrDesc, actionName }) as ReflectedActionDescriptor;
    if (actionDesc == null) return;

    if (actionDesc.MethodInfo.ReturnType == typeof (ActionResult))
    {
        // you're in
    }
}