title: MongoDB C# driver and MVC4 webapi. Issue in json serialization
body: <p>I am using MongoDB database with MVC4 WebAPI using the C# driver provided by MongoDB. I have a an issue with serialization. I get the following error,</p>
  
  <pre><code>    "ExceptionMessage=Error getting value from '__emptyInstance' on 'MongoDB.Bson.ObjectId'"
  </code></pre>
  
  <p>If I change the Content-Type to xml in my HTTP request it just works fine. I would appreciate any help.</p>
  
  <p>I have copied the model below.</p>
  
  <pre><code>public class Subscriber
  {
      public ObjectId _id;
  
      public long SubscriberId { get; set; }
  
      public Name Name { get; set; }
  
      public Address Address { get; set; }
  
      public string Phone { get; set; }
  
      public ICollection&lt;Subscription&gt; Subscription { get; set; }
  
  
      public Subscriber()
      {
          Name = new Name();
          Address = new Address();
          Subscription = new Collection&lt;Subscription&gt;();
  }
  }
  </code></pre>
  
  <p><strong>Solution</strong></p>
  
  <p>Converting _id type string and decorating the field as below did the trick</p>
  
  <pre><code>[BsonId]
  [BsonRepresentation(BsonType.ObjectId)]
  public string _id;
  </code></pre>
  
  <p>reference: <a href="https://stackoverflow.com/questions/16651776/json-net-cast-error-when-serializing-mongo-objectid">JSON.NET cast error when serializing Mongo ObjectId</a></p>