How can I code a Created-201 response using IHttpActionResult
In ASP.NET Core, an IActionResult
can be returned. This means you can return a ObjectResult
with a 201 status code.
[HttpPost]
public async Task<IActionResult> PostAsync([FromBody] CreateModel createModel)
{
// Create domain entity and persist
var entity = await _service.Create(createModel);
// Return 201
return new ObjectResult(entity) { StatusCode = StatusCodes.Status201Created };
}
If your view derives from ApiController
, you should be able to call the Created
method from base class to create such a response.
Sample:
[Route("")]
public async Task<IHttpActionResult> PostView(Guid taskId, [FromBody]View view)
{
// ... Code here to save the view
return Created(new Uri(Url.Link(ViewRouteName, new { taskId = taskId, id = view.Id })), view);
}