title: Returning ObjectID as a string from ASP.NET Core
body: <p>How to you get a string representation of the <code>ObjectId</code> returned via ASP.NET Core.</p>
  
  <p>I have the following result of an action in my controller:</p>
  
  <pre><code>return new ObjectResult(new { session, user });
  </code></pre>
  
  <p>One of the user properties is the <code>UserId</code> that is of the <code>ObjectId</code> type.</p>
  
  <p>However, this gets returned in the response as </p>
  
  <pre><code>"id": {
    "timestamp": 1482840000,
    "machine": 6645569,
    "pid": 19448,
    "increment": 5052063,
    "creationTime": "2016-12-27T12:00:00Z"
  }
  </code></pre>
  
  <p>I would like the response to simply be <code>58625d5201c4f202609fc5f3</code> that is the string representation of the same structure. </p>
  
  <p>Are there any easy way to do this for all returned <code>ObjectIds</code>?</p>
  
  <p><strong>EDIT</strong> 
  Adding some more data</p>
  
  <p>Here are the user class. <code>ObjectId</code> is <code>MongoDB.Bson.ObjectId</code></p>
  
  <pre><code>public class User
  {
      public ObjectId Id { get; set; }
      public string Name { get; set; }
      public string Email { get; set; }
      public string PasswordHash { get; set; }
      public string PasswordSalt { get; set; }
  }
  </code></pre>
  
  <p>The get method in my controller. <code>Controller</code> is <code>Microsoft.AspNetCore.Mvc.Controller</code>.</p>
  
  <pre><code>[HttpGet("{id}", Name = "GetUser")]
  public async Task&lt;IActionResult&gt; Get(ObjectId id)
  {
      var user = await _repository.GetOne&lt;User&gt;(id);
      if (user == null) return NotFound();
      return new ObjectResult(user);
  }
  </code></pre>
  
  <p>And this is the method from my repository:</p>
  
  <pre><code>public async Task&lt;T&gt; GetOne&lt;T&gt;(ObjectId id)
  {
      var collectionname = typeof(T).Name;
      var collection = _database.GetCollection&lt;T&gt;(collectionname);
      var filter = Builders&lt;T&gt;.Filter.Eq("_id", id);
      var result = await collection.Find(filter).ToListAsync();
      return result.FirstOrDefault();
  }
  </code></pre>