title: How can I access the MethodInfo.ReturnType for an ActionDescriptor within the ActionExecutingContext?
body: <p>I have an ActionFilterAttribute on my application that is used to redirect users when it detects that the user is not authenticated.  Within the filter, I would like to detect when the ReturnType for an action is a JsonResult.</p>
  
  <p>As a workaround, I initially created a custom attribute of IsJsonResult, and decorated the JsonResult methods in my solution with that attribute.  This works, and is implemented in the action filter as follows:</p>
  
  <pre><code>public class CheckUser : ActionFilterAttribute
  {
      public override void OnActionExecuting(ActionExecutingContext actionExecutingContext)
      {
          base.OnActionExecuting(actionExecutingContext);
          object[] customAttributes = actionExecutingContext.ActionDescriptor.GetCustomAttributes(true);
          bool isJsonResult = customAttributes.FirstOrDefault(a =&gt; a.GetType() == typeof(IsJsonResult)) != null;
  
          if (isJsonResult)
          {
              return; // Don't perform any additional checks on JsonResult requests.
          }
  
          // Additional checking code omitted.
      }
  }
  </code></pre>
  
  <p>That works, but I dislike the idea of decorating all of the JsonResult actions in this project.  That could easily fail if a new JsonResult gets added to the project, and we forget to decorate it accordingly.</p>
  
  <p>Moreover, I can see that the ReturnType with a name of "JsonResult" is in the debugger within the actionExecutingContext object shown above.  Here is the path shown in the watch window:</p>
  
  <pre><code>actionExecutingContext &gt; ActionDescriptor &gt; [System.Web.Mvc.ReflectedActionDescriptor] &gt; MethodInfo &gt; ReturnType &gt; FullName
  </code></pre>
  
  <p>That FullName property has a value of "System.Web.Mvc.JsonResult".</p>
  
  <p>So it would seem that I could extract that value directly from the actionExecutingContext object, and create a supporting method to return a bool indicator.  To achieve this, here is the code that I've written.</p>
  
  <pre><code>    private bool isReturnTypeJson(ActionExecutingContext actionExecutingContext)
      {
          string actionName = actionExecutingContext.ActionDescriptor.ActionName;
          string controllerName = actionExecutingContext.ActionDescriptor.ControllerDescriptor.ControllerName;
          Type controllerType = actionExecutingContext.Controller.GetType();
          try
          {
              // Only effective when the actionName is not duplicated in the controller.
              Type returnType = controllerType.GetMethod(actionName).ReturnType;
              return (returnType.Name == "JsonResult");
          }
          catch (AmbiguousMatchException)
          {
              // Using LINQ, can I filter this collection to isolate just the methods
              // that have the same name as the "actionName" variable above?
              MethodInfo[] methodsInfoCollection = controllerType.GetMethods(BindingFlags.Public | BindingFlags.Instance);
  
              // Attempted code from https://stackoverflow.com/questions/15283158/net-mvc-counting-action-methods-in-web-application
              //var info = typeof(controllerType)
              //        .Assembly.GetTypes()
              //        .Where(t =&gt; typeof(Controller).IsAssignableFrom(t))
              //        .Where(t =&gt; t.Namespace.StartsWith("AwesomeProduct.Web"))
              //        .SelectMany(t =&gt; t.GetMethods(BindingFlags.Public | BindingFlags.Instance))
              //        .Where(m =&gt; typeof(ActionResult).IsAssignableFrom(m.ReturnType))
              //        .Where(m =&gt; !m.IsAbstract)
              //        .Where(m =&gt; m.GetCustomAttribute&lt;NonActionAttribute&gt;() == null);
  
          }
          return false;
      }
  </code></pre>
  
  <p>Assigning the returnType works in the try-block whenever the action name is unique in the controller.  But if there are multiple instances of the same action name in the controller, then the AmbiguousMatchException occurs.  So in the catch-block, I've assigned the Methods to a collection. <strong>Using LINQ, how can I filter values from the methodsInfoCollection variable to the action that is arriving via the ActionExecutingContext?</strong></p>
  
  <p>Some of the articles that I've researched are below, and have used some ideas from those.  But I've yet to figure this out.</p>
  
  <ul>
  <li><a href="http://www.codeproject.com/Articles/742461/Csharp-Using-Reflection-and-Custom-Attributes-to-M" rel="noreferrer">http://www.codeproject.com/Articles/742461/Csharp-Using-Reflection-and-Custom-Attributes-to-M</a></li>
  <li><a href="https://stackoverflow.com/questions/2865061/can-i-get-an-actions-return-type-from-an-action-filter">Can I get an Action&#39;s return type from an Action Filter?</a></li>
  <li><a href="https://stackoverflow.com/questions/15283158/net-mvc-counting-action-methods-in-web-application">.NET MVC: Counting Action methods in web application</a></li>
  </ul>
  
  <p>Thanks for your help.</p>
  
  <p>==============</p>
  
  <p>Update to original code. This works, but the looping is not ideal.</p>
  
  <pre><code>private bool isReturnTypeJson(ActionExecutingContext actionExecutingContext)
  {
      string actionName = actionExecutingContext.ActionDescriptor.ActionName;
      string controllerName = actionExecutingContext.ActionDescriptor.ControllerDescriptor.ControllerName;
      Type controllerType = actionExecutingContext.Controller.GetType();
      try
      {
          // Only effective when the actionName is not duplicated in the controller.
          Type returnType = controllerType.GetMethod(actionName).ReturnType;
          return (returnType.Name == "JsonResult");
      }
      catch (AmbiguousMatchException)
      {
          // Using LINQ, can I filter this collection to isolate just the methods with a name of actionName.
          MethodInfo[] methodInfoCollection = controllerType.GetMethods(BindingFlags.Public | BindingFlags.Instance);
          foreach (MethodInfo methodInfo in methodInfoCollection)
          {
              if (methodInfo.ReturnType != null) {
                  if (methodInfo.ReturnType == typeof(ActionResult))
                  {
                      return false;
                  }
                  if (methodInfo.ReturnType == typeof(JsonResult))
                  {
                      return true;
                  }
              }
          }
      }
      return false;
  }
  </code></pre>
  
  <p>See the solution from Reza Aghaei below.  His solution totally eliminates the need for a separate method.</p>