title: Web Api Model Binding and Polymorphic Inheritance
body: <p>I am asking if anyone knows if it is possible to to pass into a Web Api a concrete class that inherits from a abstract class.</p>
  <p>For example:</p>
  <pre><code>public abstract class A
  {
      A();
  }
  
  public class B : A
  {
  }
  
  [POST(&quot;api/Request/{a}&quot;)]
  public class Request(A a)
  {
  }
  </code></pre>
  <p>At present I have looked around and most solutions seem to say that using TypeNameHandling will work.</p>
  <pre><code>JsonMediaTypeFormatter jsonFormatter = new JsonMediaTypeFormatter();
  jsonFormatter.SerializerSettings.TypeNameHandling = TypeNameHandling.Auto;
  </code></pre>
  <p>However this is not that case. Also my model is being passed from a console app to the webapi. I have read that I may be able to deserialize the json object and after attempting this a few times I decide this was not going to work.</p>
  <p>I have looked into creating a customer model binder however, I do not want to make my application more complex that it has to be. At present I inherit from the abstract class with 3 models but may in the future extend this. As you may note adding custom model binders may require multiple binders unless there is a way of making one binder generic for all types of the abstract class.</p>
  <p>To expand on this in my console app I have instantiated class b as such and then passed it to the ObjectContent before posting to my webapi</p>
  <pre><code>item = B();
  
  //serialize and post to web api
  MediaTypeFormatter formatter;
  JsonMediaTypeFormatter jsonFormatter = new JsonMediaTypeFormatter();
  jsonFormatter.SerializerSettings.TypeNameHandling = TypeNameHandling.Auto;
  formatter = jsonFormatter;
  
  _content = new ObjectContent&lt;A&gt;(item, formatter);
  var response = _client.PostAsync(&quot;api/Request&quot;, _content).Result;
  </code></pre>
  <p>when the webapi action is called the object is null</p>