title: Detecting Extra Fields in ASP.NET Web API Request
body: <p>I have been trying to get the example from <a href="http://lbadri.wordpress.com/2014/01/28/detecting-extra-fields-in-asp-net-web-api-request/" rel="nofollow">this</a> link to work.</p>
  
  <p>I'm trying to detect extra fields in a json request and return an error if they are present.</p>
  
  <p>Here's what I have:</p>
  
  <p>ApiController:</p>
  
  <pre><code>public class MyClassController : ApiController
  {
      public IHttpActionResult Add(MyClass myClass)
      {
          if (myClass.ContainsExtra)
          {
              return BadRequest(ModelState);
          }
          ...
       }
  ...
  }
  </code></pre>
  
  <p>DynamicObject:</p>
  
  <pre><code>public class MyClass : DynamicObject
  {
      private Dictionary&lt;string, object&gt; fields =
                          new Dictionary&lt;string, object&gt;(
                               StringComparer.OrdinalIgnoreCase);
  
      [Required]
      public string FirstName { get; set; }
  
      [Required]
      public string LastName { get; set; }
  
      public bool ContainsExtra { get; private set; }
  
      [JsonIgnore]
      public Dictionary&lt;string, object&gt; Extra
      {
          get { return fields; }
      }
  
      public override bool TryGetMember(GetMemberBinder binder,
                                                out object value)
      {
          return fields.TryGetValue(binder.Name, out value);
      }
  
      public override bool TrySetMember(SetMemberBinder binder,
                                                    object value)
      {
          this.ContainsExtra = true;
  
          fields[binder.Name] = value;
          return true;
      }
  }
  </code></pre>
  
  <p>If I send this Json from Fiddler</p>
  
  <pre><code>{“FirstName”:”Test”, “LastName”:”Test”, “City”:”New York”}
  </code></pre>
  
  <p>The TrySetMember method should fire and it should set the bool ContainsExtra to true so it can be evaluated in the Add method on MyClassController. When it does contain an extra field, it should return an error to the client.</p>
  
  <p>Unfortunately I just can't seem to get the TrySetMember to fire.</p>
  
  <p>What am I Missing ? </p>