title: Attribute.IsDefined returns false, Attribute.GetCustomAttribute returns null for defined ActionNameAttribute
body: <p>I'm trying to set up some unit tests to ensure that URLs will be mapped to the appropriate controllers and actions according to a route-table, and that the target action-method and controller exists within the relevant assembly.</p>
  
  <p>The only remaining problem I'm having is testing the existence of an action-method where an <code>ActionNameAttribute</code> has been applied to enable dash-separated action-name mappings, e.g., a "Contact Us" form url: <code>/contact-us</code> maps to the ContactUs method on a Forms controller because the ContactUs method signature is defined thusly:</p>
  
  <pre><code>[ActionName("contact-us")]
  public ActionResult ContactUs()
  </code></pre>
  
  <p>I've set up the following method, which I am running inside each test, and works for all cases where the action-method name is <em>not</em> redefined with <code>ActionNameAttribute</code>:</p>
  
  <pre><code>private static bool ActionIsDefinedOnController(string expectedActionName, string controllerName, string assemblyName)
  {
      var thisControllerType = Type.GetType(AssemblyQualifiedName(controllerName, assemblyName), false, true);
  
      if (thisControllerType == null)
          return false;
  
      var allThisControllersActions = thisControllerType.GetMethods().Select(m =&gt; m.Name.ToLower());
  
      if( allThisControllersActions.Contains(expectedActionName.ToLower()))
          return true;
  
      var methods = thisControllerType.GetMethods();
  
      //If we've so far failed to find the method, look for methods with ActionName attributes, and check in those values:
      foreach (var method in methods)
      {
          if (Attribute.IsDefined(method, typeof(ActionNameAttribute)) 
          {
              var a = (ActionNameAttribute) Attribute.GetCustomAttribute(method, typeof (ActionNameAttribute));
              if (a.Name == expectedActionName)
                  return true;
          }
      }
      return false;
  }
  </code></pre>
  
  <p>...but whenever a method's name is redifined with <code>ActionNameAttribute</code>, the check <code>Attribute.IsDefined(method, typeof(ActionNameAttribute)</code> is failing (returns <code>false</code>), even when I can see the attribute in the list of custom-attributes in my debugging session:</p>
  
  <p><img src="https://i.stack.imgur.com/y8wgb.jpg" alt="Failure of Action.IsDefined"></p>
  
  <p><strong>Why is this check failing, when it should be passing?</strong></p>
  
  <p>I've been able to construct a different check:</p>
  
  <p><strong>UPDATE</strong> I had pasted in the wrong code here initially, here's the revised:</p>
  
  <pre><code>List&lt;string&gt; customAttributes = method.GetCustomAttributes(false).Select(a =&gt; a.ToString()).ToList();
  
  if (customAttributes.Contains("System.Web.Mvc.ActionNameAttribute")) 
  {
      var a = (ActionNameAttribute) Attribute.GetCustomAttribute(method, typeof (ActionNameAttribute));
      if (a.Name == expectedActionName)
          return true;
  }
  </code></pre>
  
  <p>...and now my condition is catching the cases where <code>ActionNameAttribute</code> is applied, but now <code>Attribute.GetCustomAttribute()</code> returns null.    So I can't check the value of the action name to compare against the expected value...  arrrrgh!</p>