在你的试试这个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'
更新:
由于您使用的是该OnResultExecuted
方法,请尝试以下操作:
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'
}
更新:
根据您的评论,如果有多个Action
同名(重载):
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
}
}