title: Correct way to disable model validation in ASP.Net Core 2 MVC
body: <p>Set up MVC with the extension method</p>
  
  <pre><code>services.AddMvc()
  </code></pre>
  
  <p>Then in a controller, and this may apply to GET also, create a method for the POST action with a parameter supplied in the body, e.g.</p>
  
  <pre><code>[HttpPost("save")]
  public Entity Save([FromBody]Entity someEntity)
  </code></pre>
  
  <p>When the action is called the MVC pipeline will call the ParameterBinder which in turn calls <code>DefaultObjectValidator</code>.  I don't want the validation (its slow for one thing, but more importantly is looping on complex cyclical graphs), but it seems the only way to turn off validation in the pipeline is something like this:</p>
  
  <pre><code>public class NonValidatingValidator : IObjectModelValidator
  {
      public void Validate(ActionContext actionContext, ValidationStateDictionary validationState, string prefix, object model)
      {
      }
  }
  </code></pre>
  
  <p>and in the StartUp/ConfigureServices:</p>
  
  <pre><code>        var validator = services.FirstOrDefault(s =&gt; s.ServiceType == typeof(IObjectModelValidator));
          if (validator != null)
          {
              services.Remove(validator);
              services.Add(new ServiceDescriptor(typeof(IObjectModelValidator), _ =&gt; new NonValidatingValidator(), ServiceLifetime.Singleton));
          }
  </code></pre>
  
  <p>which seems like a sledgehammer.  I've looked around and can't find an alternative, also tried to remove the <code>DataAnnotationModelValidator</code> without success, so would like to know if there's a better/correct way to turn off validation?</p>