title: ASP.NET Core MVC Mixed Route/FromBody Model Binding & Validation
body: <p>I am using ASP.NET Core 1.1 MVC to build an JSON API.  Given the following model and action method:</p>
  
  <pre><code>public class TestModel
  {
      public int Id { get; set; }
  
      [Range(100, 999)]
      public int RootId { get; set; }
  
      [Required, MaxLength(200)]
      public string Name { get; set; }
  
      public string Description { get; set; }
  }
  
  [HttpPost("/test/{rootId}/echo/{id}")]
  public IActionResult TestEcho([FromBody] TestModel data)
  {
      return Json(new
      {
          data.Id,
          data.RootId,
          data.Name,
          data.Description,
          Errors = ModelState.IsValid ? null : ModelState.SelectMany(x =&gt; x.Value.Errors)
      });
  }
  </code></pre>
  
  <p>The <code>[FromBody]</code> on my action method parameter is causing the model to be bound from the JSON payload that is posted to the endpoint, however it also prevents the <code>Id</code> and <code>RootId</code> properties from being bound via the route parameters.</p>
  
  <p>I could break this up into to separate models, one bound from the route and one from the body or I could also force any clients to send the <code>id</code> &amp; <code>rootId</code> as part of the payload, but both of those solutions seem to complicate things more than I'd like and don't allow me to keep the validation logic in a single place.  Is there any way to get this situation working where the model can be bound properly and I can keep my model &amp; validation logic together?</p>