AspNetCore.Docs/aspnetcore/includes/webApi/end.md

2.6 KiB

Implement the other CRUD operations

We'll add Create, Update, and Delete methods to the controller. These are variations on a theme, so I'll just show the code and highlight the main differences. Build the project after adding or changing code.

Create

[!code-csharpMain]

This is an HTTP POST method, indicated by the [HttpPost] attribute. The [FromBody] attribute tells MVC to get the value of the to-do item from the body of the HTTP request.

The CreatedAtRoute method returns a 201 response, which is the standard response for an HTTP POST method that creates a new resource on the server. CreatedAtRoute also adds a Location header to the response. The Location header specifies the URI of the newly created to-do item. See 10.2.2 201 Created.

Use Postman to send a Create request

Postman console

  • Set the HTTP method to POST
  • Select the Body radio button
  • Select the raw radio button
  • Set the type to JSON
  • In the key-value editor, enter a Todo item such as
{
	"name":"walk dog",
	"isComplete":true
}
  • Select Send

  • Select the Headers tab in the lower pane and copy the Location header:

Headers tab of the Postman console

You can use the Location header URI to access the resource you just created. Recall the GetById method created the "GetTodo" named route:

[HttpGet("{id}", Name = "GetTodo")]
public IActionResult GetById(long id)

Update

[!code-csharpMain]

Update is similar to Create, but uses HTTP PUT. The response is 204 (No Content). According to the HTTP spec, a PUT request requires the client to send the entire updated entity, not just the deltas. To support partial updates, use HTTP PATCH.

Postman console showing 204 (No Content) response

Delete

[!code-csharpMain]

The response is 204 (No Content).

Postman console showing 204 (No Content) response