title: Suppress RequiredAttribute validation for the JsonMediaTypeFormatter in ASP.NET Web API
body: <p>Assuming I sent the following request to my API:</p>
  
  <pre><code>POST http://localhost:4940/api/cars HTTP/1.1
  User-Agent: Fiddler
  Host: localhost:4940
  Content-Type: application/json
  Content-Length: 44
  
  {"Make":"Make1","Year":2010,"Price":10732.2}
  </code></pre>
  
  <p>And I have the following Car class definition:</p>
  
  <pre><code>public class Car {
  
      public int Id { get; set; }
  
      [Required]
      [StringLength(20)]
      public string Make { get; set; }
  
      [Required]
      [StringLength(20)]
      public string Model { get; set; }
  
      public int Year { get; set; }
  
      [Range(0, 500000)]
      public float Price { get; set; }
  }
  </code></pre>
  
  <p>The response I got back is as follows:</p>
  
  <pre><code>HTTP/1.1 400 Bad Request
  Cache-Control: no-cache
  Pragma: no-cache
  Content-Type: application/json; charset=utf-8
  Expires: -1
  Server: Microsoft-IIS/8.0
  X-AspNet-Version: 4.0.30319
  X-SourceFiles: =?UTF-8?B?RTpcRHJvcGJveFxCb29rc1xQcm9XZWJBUEkuU2FtcGxlc1xDaGFwdGVyMTNcRGF0YUFubm90YXRpb25WYWxpZGF0aW9uQXR0cmlidXRlc1NhbXBsZVxEYXRhQW5ub3RhdGlvblZhbGlkYXRpb25BdHRyaWJ1dGVzU2FtcGxlXGFwaVxjYXJz?=
  X-Powered-By: ASP.NET
  Date: Mon, 17 Sep 2012 11:38:58 GMT
  Content-Length: 182
  
  {"Message":"The request is invalid.","ModelState":{"car":["Required property 'Model' not found in JSON. Path '', line 1, position 44."],"car.Model":["The Model field is required."]}}
  </code></pre>
  
  <p>Here is the more readable form of the message body:</p>
  
  <pre><code>{
      "Message": "The request is invalid.",
      "ModelState": {
          "car": [
              "Required property 'Model' not found in JSON. Path '', line 1, position 44."
          ],
          "car.Model":[
              "The Model field is required."
          ]
      }
  }
  </code></pre>
  
  <p>As you can see, I also have one additional error message for car and I am guessing the <code>JsonMediaTypeFormatter</code> performs validation as well and it fails to perform the read action.</p>
  
  <p>Is this the issue here? Is there any way I can suppress the validation at the JsonMediaTypeFormatter level? I don't want the formatter to perform validation because the validation is also performed by the <code>IBodyModelValidator</code> after the formatter reads the message body.</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>I debugged the source code and it tuns out the <code>JsonMediaTypeFormatter</code> throws an error if a Property is marked with Required and not supplied. the following code is a part from the <code>JsonMediaTypeFormatter</code>:</p>
  
  <pre><code>// Error must always be marked as handled
  // Failure to do so can cause the exception to be rethrown at every recursive level and overflow the stack for x64 CLR processes
  jsonSerializer.Error += (sender, e) =&gt;
  {
      Exception exception = e.ErrorContext.Error;
      formatterLogger.LogError(e.ErrorContext.Path, exception);
      e.ErrorContext.Handled = true;
  }
  </code></pre>
  
  <p>and this triggers the <code>ModelStateFormatterLogger.LogError</code> method which puts the error inside the <code>ModelState</code>:</p>
  
  <pre><code>public void LogError(string errorPath, Exception exception)
  {
      if (errorPath == null)
      {
          throw Error.ArgumentNull("errorPath");
      }
      if (exception == null)
      {
          throw Error.ArgumentNull("exception");
      }
  
      string key = ModelBindingHelper.ConcatenateKeys(_prefix, errorPath);
      _modelState.AddModelError(key, exception);
  }
  </code></pre>
  
  <p>I am still unable to suppress this behavior.</p>