title: Returning the URI of a newly created resource in ASP.NET Core 2.2
body: <p>I would like to return the location URI of a newly created resource in ASP.NET Core 2.2. Here is the method to get a Food Item for a Food Vendor:</p>
  
  <pre><code>[HttpGet("{foodVendorId}/foodItems/{foodItemId}", Name = "GetFoodItem")]
  public IActionResult GetFoodVendorFoodItem(int foodVendorId, int foodItemId)
  {
      if (!_foodDeliveryAPIRepository.FoodVendorExists(foodVendorId))
      {
          return NotFound();
      }
  
      var foodItem = _foodDeliveryAPIRepository.GetFoodItemForFoodVendor(foodVendorId, foodItemId);
  
      if (foodItem == null)
      {
          return NotFound();
      }
  
      var foodItemResult = Mapper.Map&lt;FoodItemDto&gt;(foodItem);
  
      return Ok(foodItemResult);
  }
  </code></pre>
  
  <p>And here is the method to add or create a food item for a Food Vendor:</p>
  
  <pre><code>[HttpPost("{foodVendorId}/foodItems")]
  public IActionResult CreateFoodItem(int foodVendorId, [FromBody] FoodItemForCreationDto foodItem)
  {
      if (foodItem == null)
      {
          return BadRequest();
      }
  
      if (!_foodDeliveryAPIRepository.FoodVendorExists(foodVendorId))
      {
          return NotFound();
      }
  
      var finalFoodItem = Mapper.Map&lt;Entities.FoodItem&gt;(foodItem);
  
      _foodDeliveryAPIRepository.AddFoodItemForFoodVendor(foodVendorId, finalFoodItem);
  
      if (!_foodDeliveryAPIRepository.Save())
      {
          return StatusCode(500, "A problem happened while handling your request.");
      }
  
      var createdFoodItemToReturn = Mapper.Map&lt;Models.FoodItemDto&gt;(finalFoodItem);
  
      return CreatedAtRoute("GetFoodItem", new
          { foodVendorId = foodVendorId, id = createdFoodItemToReturn.Id }, createdFoodItemToReturn);
  }
  </code></pre>
  
  <p>Upon creating the FoodItem, the values gets to the database but it returns a 500 server error after creation. After debugging it, I realize the error is in the return statement of the CreatedAtRoute - </p>
  
  <pre><code>return CreatedAtRoute("GetFoodItem", new
          { foodVendorId = foodVendorId, id = createdFoodItemToReturn.Id }, createdFoodItemToReturn);
  </code></pre>
  
  <p>What am I not doing right?
  - <a href="https://stackoverflow.com/questions/31957060/mvc-6-web-api-resolving-the-location-header-on-a-201-created">This</a> didn't solve my problem.</p>