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

66 lines
2.7 KiB
Markdown
Raw Normal View History

## Implement the other CRUD operations
In the following sections, `Create`, `Update`, and `Delete` methods are added to the controller.
### Create
Add the following `Create` method.
[!code-csharp[](../../tutorials/first-web-api/sample/TodoApi/Controllers/TodoController.cs?name=snippet_Create)]
The preceding code is an HTTP POST method, indicated by the [`[HttpPost]`](/aspnet/core/api/microsoft.aspnetcore.mvc.httppostattribute) attribute. The [`[FromBody]`](/aspnet/core/api/microsoft.aspnetcore.mvc.frombodyattribute) 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. HTTP 201 is the standard response for an HTTP POST method that creates a new resource on the server.
* 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](http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html).
* Uses the "GetTodo" named route to create the URL. The "GetTodo" named route is defined in `GetById`:
[!code-csharp[](../../tutorials/first-web-api/sample/TodoApi/Controllers/TodoController.cs?name=snippet_GetByID&highlight=1-2)]
### Use Postman to send a Create request
![Postman console](../../tutorials/first-web-api/_static/pmc.png)
* 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
```json
{
"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](../../tutorials/first-web-api/_static/pmget.png)
The Location header URI can be used to access the new item.
### Update
Add the following `Update` method:
[!code-csharp[](../../tutorials/first-web-api/sample/TodoApi/Controllers/TodoController.cs?name=snippet_Update)]
`Update` is similar to `Create`, but uses HTTP PUT. The response is [204 (No Content)](http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html). 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](../../tutorials/first-web-api/_static/pmcput.png)
### Delete
Add the following `Delete` method:
[!code-csharp[](../../tutorials/first-web-api/sample/TodoApi/Controllers/TodoController.cs?name=snippet_Delete)]
The `Delete` response is [204 (No Content)](http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html).
Test `Delete`:
![Postman console showing 204 (No Content) response](../../tutorials/first-web-api/_static/pmd.png)