title: How to use Created (or CreatedAtAction / CreatedAtRoute) in an asp net core api
body: <p>I'd like to inform consumers of my api about the location of the newly created object. I know there is <code>Created()</code> <code>CreatedAtRoute()</code> and <code>CreatedAtAction()</code> but I am unsure how to use it.</p>
  
  <p>Here is what I've tried: </p>
  
  <p>I have a <strong>Get</strong> resource to which I would like to point. It takes an ID as input:</p>
  
  <pre><code>    [HttpGet("/${id}", Name = "GetProduct")]
      [ProducesResponseType(typeof(Produkt), 200)]
      public IActionResult Get([FromRoute] int id)
      {
         // some code...
          return Ok(...);
      }
  </code></pre>
  
  <p>When a product gets created via my <strong>POST</strong> route, I would like to point to this Resource via the Location header:</p>
  
  <p><strong>Attempt 1</strong></p>
  
  <pre><code>    [HttpPost]
      [ProducesResponseType(typeof(Produkt), 200)]
      public IActionResult CreateNewProduct([FromBody] ProduktDtoForCreate productFromBody)
      {
          //...
          return CreatedAtRoute("GetProduct", new { id = productToCreate.Id }, productToCreate);
      }
  </code></pre>
  
  <p>This returns a <strong>Location Header</strong> of: <strong><a href="http://localhost:5000/" rel="noreferrer">http://localhost:5000/</a>$15003</strong></p>
  
  <p><strong>Attempt 2</strong></p>
  
  <pre><code>    [HttpPost]
      [ProducesResponseType(typeof(Produkt), 200)]
      public IActionResult CreateNewProduct([FromBody] ProduktDtoForCreate productFromBody)
      {
          //...
          return Created(new Uri($"{Request.Path}/{productToCreate.Id}", UriKind.Relative), productToCreate);
      }
  </code></pre>
  
  <p>This one works and returns <strong>/api/v1.0/produkte/16004</strong> but it seems like using the current request to point to the new location should not be needed. Also I am not sure if this is good practice?</p>