title: ASP.net 5 Web API Post CreatedAtRoute always returns 500 Internal Server Error
body: <p>The database works. It does actually insert the new record, but when I use CreatedAtRoute(), I always get a 500 back from the client. Why?</p>
  
  <p>My controller's Get:</p>
  
  <pre class="lang-cs prettyprint-override"><code>[Route("api/[controller]")]
  public class IngredientController : Controller
  {
      private SimpleCookbookDbContext db { get; set; }
  
      public IngredientController(SimpleCookbookDbContext context)
      {
          db = context;
      }
  
      // GET: api/values
      [HttpGet]
      public async Task&lt;IEnumerable&lt;Ingredient&gt;&gt; Get()
      {
          return await db.Ingredients.ToListAsync();
      }
  
      // GET api/values/5
      [HttpGet("{id}", Name = "GetIngredient")]
      public async Task&lt;Ingredient&gt; Get(int id)
      {
          return await db.Ingredients.SingleOrDefaultAsync(i =&gt; i.Id == id);
      }
  
      [HttpPost]
      public async Task&lt;IActionResult&gt; Post([FromBody]Ingredient ingredient)
      {
          try
          {
  
              var res = await IM.CreateAsync(ingredient);
  
              if (!res.Success)
              {
                  return HttpBadRequest(res.Errors);
              }
  
          }
          catch(Exception)
          {
              return new HttpStatusCodeResult((int)HttpStatusCode.InternalServerError);
          }
  
  
          return CreatedAtRoute("GetIngredient", new { controller="Ingredient", id = ingredient.Id });
      }
  }
  </code></pre>
  
  <p>I tried debugging this. Yes, it would return the HttpBadRequest if the ingredient I'm trying to insert already exists.</p>
  
  <p>I tried putting a breakpoint inside the catch block, and I'm not getting there, so I assume there was no error from the database.</p>
  
  <p>The record does get inserted to the database. I do get to the line <code>return CreatedAtRoute(...);</code> but I get a 500 back. (I set a breakpoint there, too).</p>
  
  <p>Now, I'm using fiddler. My request is this:</p>
  
  <pre><code>POST /api/ingredient HTTP/1.1
  Host: localhost:55303
  Content-Type: application/json;charset=utf-8
  
  {"id":0, "name": "rosemary", "description": "rosemary"}
  </code></pre>
  
  <p>I also removed the double quotes on the property names, and I still get the same 500.</p>
  
  <p>I do have camel-casing resolved at the Startup:</p>
  
  <pre class="lang-cs prettyprint-override"><code>services.AddMvc().AddJsonOptions(options =&gt; {
                  options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
              });
  </code></pre>
  
  <p>I think I showed all relevant code. If you need more, please let me know.</p>
  
  <hr>
  
  <p><strong>UPDATE</strong></p>
  
  <p>CreatedAtRoute has an overload taking in three parameters:</p>
  
  <pre><code>return CreatedAtRoute("GetIngredient", new { controller="Ingredient", id = ingredient.Id }, ingredient);
  </code></pre>
  
  <p>The last parameter is an object, which you could create dynamically or pass back your entire entity, depending on what you want to expose back.</p>
  
  <p>It's strange how there's a 2-parameter variant that would result in a strange 500 response. </p>