One of the most common ways to return a 201 HTTP status code in web API is to return Created(). However, this method requires providing an URI.
[HttpPost]
public async Task<IActionResult> Create(CreateBookModel book)
{
// Logic to create book
var createdBookUri = "/books/1"; // Sample url to new book
return Created(createdBookUri, book);
}
If you only want to return 201 HTTP status code without URI, you can return ObjectResult instead
[HttpPost]
public async Task<IActionResult> Create(CreateBookModel book)
{
// Logic to create book
return new ObjectResult(book)
{
StatusCode = StatusCodes.Status201Created
};
}