title: How to detect duplicate keys in Web Api Post request Json
body: <p>I have a requirement to return a 400 error from an ASP.NET Web API Post request when the request Json contains duplicate keys.</p>
  
  <p>For instance if the request was</p>
  
  <pre><code>{
     "key1": "value1",
     "key2": 1000,
     "key2": 2000,
     "key3": "value3"
  }
  </code></pre>
  
  <p>then I would want the error to be thrown due to there being two "key2" keys.</p>
  
  <p>My controller method looks something like</p>
  
  <pre><code>[HttpPost]
  public IHttpActionResult PostMethod([FromBody]RequestModel request)
  {
     .....
  }
  </code></pre>
  
  <p>and my RequestModel model like</p>
  
  <pre><code>public class RequestModel
  {
      [Required]
      public string Key1 {get; set; }
  
      [Required]
      public int Key2 {get; set; }
  
      public string Key3 {get; set; } 
  }
  </code></pre>
  
  <p>In the example above the Json serializer seems happy to accept the request and populate Key2 with 2000, or whatever the last instance of the key is.</p>
  
  <p>I am thinking I need to do something involving the JsonSerializerSettings class, or implement a custom JsonConverter, however I am unsure how to proceed.</p>