Short Answer
You are using the wrong overload of CreatedAtRoute
. Use the overload that takes three arguments instead.
Working Example
For instance, the following works on my machine.
[Route("api/[controller]")]
public class SubscribersController : Controller
{
public IActionResult Index()
{
var subscriber = new
{
Id = Guid.NewGuid(),
FirstName = "Shaun",
LastName = "Luttin"
};
// overload with three arguments
return CreatedAtRoute(
routeName: "SubscriberLink",
routeValues: new { id = subscriber.Id },
value: subscriber);
}
[HttpGet("{id}", Name = "SubscriberLink")]
public IActionResult GetSubscriber(Guid id)
{
var subscriber = new
{
Id = id,
FirstName = "Shaun",
LastName = "Luttin"
};
return new JsonResult(subscriber);
}
}
Details
The result is a 201 response. The response’s body contains details of the entity we created (the value
argument) and the response’s Location header contains a URI to the entity.
There are three overloads for CreatedAtRoute
.
CreatedAtRoute(object routeValues, object value)
CreatedAtRoute(string routeName, object value)
CreatedAtRoute(string routeName, object routeValues, object value)
If we want to pass a route name and route values, we use the overload that takes three arguments. If we do not want to return details of the entity we created, we can pass null
for the third argument,