Skip to content

Commit 835c377

Browse files
Copilotwadepickett
andcommitted
Add PATCH endpoint to minimal API tutorial for .NET 7, 8, and 9
Co-authored-by: wadepickett <[email protected]>
1 parent c9ae771 commit 835c377

File tree

6 files changed

+218
-0
lines changed

6 files changed

+218
-0
lines changed

aspnetcore/tutorials/min-web-api.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ This tutorial creates the following API:
3333
| `GET /todoitems/{id}` | Get an item by ID | None | To-do item |
3434
| `POST /todoitems` | Add a new item | To-do item | To-do item |
3535
| `PUT /todoitems/{id}` | Update an existing item &nbsp; | To-do item | None |
36+
| `PATCH /todoitems/{id}` | Partially update an item &nbsp;| Partial to-do item | None |
3637
| `DELETE /todoitems/{id}` &nbsp; &nbsp; | Delete an item &nbsp; &nbsp; | None | None |
3738

3839
## Prerequisites
@@ -513,6 +514,73 @@ Use Swagger to send a PUT request:
513514

514515
---
515516

517+
## Examine the PATCH endpoint
518+
519+
The sample app implements a single PATCH endpoint using `MapPatch`:
520+
521+
[!code-csharp[](~/tutorials/min-web-api/samples/9.x/todo/Program.cs?name=snippet_patch)]
522+
523+
This method is similar to the `MapPut` method, except it uses HTTP PATCH and only updates the fields provided in the request. A successful response returns [204 (No Content)](https://www.rfc-editor.org/rfc/rfc9110#status.204). According to the HTTP specification, a PATCH request enables partial updates, allowing clients to send only the fields that need to be changed.
524+
525+
> [!NOTE]
526+
> PATCH operations allow partial updates to resources. For more advanced partial updates using JSON Patch documents, see <xref:web-api/jsonpatch>.
527+
528+
## Test the PATCH endpoint
529+
530+
This sample uses an in-memory database that must be initialized each time the app is started. There must be an item in the database before you make a PATCH call. Call GET to ensure there's an item in the database before making a PATCH call.
531+
532+
Update only the `name` property of the to-do item that has `Id = 1` and set its name to `"run errands"`.
533+
534+
# [Visual Studio](#tab/visual-studio)
535+
536+
* In **Endpoints Explorer**, right-click the **PATCH** endpoint, and select **Generate request**.
537+
538+
The following content is added to the `TodoApi.http` file:
539+
540+
```http
541+
PATCH {{TodoApi_HostAddress}}/todoitems/{id}
542+
543+
###
544+
```
545+
546+
* In the PATCH request line, replace `{id}` with `1`.
547+
548+
* Add the following lines immediately after the PATCH request line:
549+
550+
```http
551+
Content-Type: application/json
552+
553+
{
554+
"name": "run errands"
555+
}
556+
```
557+
558+
The preceding code adds a Content-Type header and a JSON request body with only the field to update.
559+
560+
* Select the **Send request** link that is above the new PATCH request line.
561+
562+
The PATCH request is sent to the app and the response is displayed in the **Response** pane. The response body is empty, and the status code is 204.
563+
564+
# [Visual Studio Code](#tab/visual-studio-code)
565+
566+
Use Swagger to send a PATCH request:
567+
568+
* Select **Patch /todoitems/{id}** > **Try it out**.
569+
570+
* Set the **id** field to `1`.
571+
572+
* Set the request body to the following JSON:
573+
574+
```json
575+
{
576+
"name": "run errands"
577+
}
578+
```
579+
580+
* Select **Execute**.
581+
582+
---
583+
516584
## Examine and test the DELETE endpoint
517585

518586
The sample app implements a single DELETE endpoint using `MapDelete`:

aspnetcore/tutorials/min-web-api/includes/min-web-api6-7.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ This tutorial creates the following API:
1515
| `GET /todoitems/{id}` | Get an item by ID | None | To-do item |
1616
| `POST /todoitems` | Add a new item | To-do item | To-do item |
1717
| `PUT /todoitems/{id}` | Update an existing item &nbsp; | To-do item | None |
18+
| `PATCH /todoitems/{id}` | Partially update an item &nbsp;| Partial to-do item | None |
1819
| `DELETE /todoitems/{id}` &nbsp; &nbsp; | Delete an item &nbsp; &nbsp; | None | None |
1920

2021
## Prerequisites
@@ -320,6 +321,39 @@ Use Swagger to send a PUT request:
320321

321322
* Select **Execute**.
322323

324+
## Examine the PATCH endpoint
325+
326+
The sample app implements a single PATCH endpoint using `MapPatch`:
327+
328+
[!code-csharp[](~/tutorials/min-web-api/samples/7.x/todo/Program.cs?name=snippet_patch)]
329+
330+
This method is similar to the `MapPut` method, except it uses HTTP PATCH and only updates the fields provided in the request. A successful response returns [204 (No Content)](https://www.rfc-editor.org/rfc/rfc9110#status.204). According to the HTTP specification, a PATCH request enables partial updates, allowing clients to send only the fields that need to be changed.
331+
332+
> [!NOTE]
333+
> PATCH operations allow partial updates to resources. For more advanced partial updates using JSON Patch documents, see <xref:web-api/jsonpatch>.
334+
335+
## Test the PATCH endpoint
336+
337+
This sample uses an in-memory database that must be initialized each time the app is started. There must be an item in the database before you make a PATCH call. Call GET to ensure there's an item in the database before making a PATCH call.
338+
339+
Update only the `name` property of the to-do item that has `Id = 1` and set its name to `"run errands"`.
340+
341+
Use Swagger to send a PATCH request:
342+
343+
* Select **Patch /todoitems/{id}** > **Try it out**.
344+
345+
* Set the **id** field to `1`.
346+
347+
* Set the request body to the following JSON:
348+
349+
```json
350+
{
351+
"name": "run errands"
352+
}
353+
```
354+
355+
* Select **Execute**.
356+
323357
## Examine and test the DELETE endpoint
324358

325359
The sample app implements a single DELETE endpoint using `MapDelete`:

aspnetcore/tutorials/min-web-api/includes/min-web-api8.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ This tutorial creates the following API:
1515
| `GET /todoitems/{id}` | Get an item by ID | None | To-do item |
1616
| `POST /todoitems` | Add a new item | To-do item | To-do item |
1717
| `PUT /todoitems/{id}` | Update an existing item &nbsp; | To-do item | None |
18+
| `PATCH /todoitems/{id}` | Partially update an item &nbsp;| Partial to-do item | None |
1819
| `DELETE /todoitems/{id}` &nbsp; &nbsp; | Delete an item &nbsp; &nbsp; | None | None |
1920

2021
## Prerequisites
@@ -491,6 +492,73 @@ Use Swagger to send a PUT request:
491492

492493
---
493494

495+
## Examine the PATCH endpoint
496+
497+
The sample app implements a single PATCH endpoint using `MapPatch`:
498+
499+
[!code-csharp[](~/tutorials/min-web-api/samples/8.x/todo/Program.cs?name=snippet_patch)]
500+
501+
This method is similar to the `MapPut` method, except it uses HTTP PATCH and only updates the fields provided in the request. A successful response returns [204 (No Content)](https://www.rfc-editor.org/rfc/rfc9110#status.204). According to the HTTP specification, a PATCH request enables partial updates, allowing clients to send only the fields that need to be changed.
502+
503+
> [!NOTE]
504+
> PATCH operations allow partial updates to resources. For more advanced partial updates using JSON Patch documents, see <xref:web-api/jsonpatch>.
505+
506+
## Test the PATCH endpoint
507+
508+
This sample uses an in-memory database that must be initialized each time the app is started. There must be an item in the database before you make a PATCH call. Call GET to ensure there's an item in the database before making a PATCH call.
509+
510+
Update only the `name` property of the to-do item that has `Id = 1` and set its name to `"run errands"`.
511+
512+
# [Visual Studio](#tab/visual-studio)
513+
514+
* In **Endpoints Explorer**, right-click the **PATCH** endpoint, and select **Generate request**.
515+
516+
The following content is added to the `TodoApi.http` file:
517+
518+
```
519+
PATCH {{TodoApi_HostAddress}}/todoitems/{id}
520+
521+
###
522+
```
523+
524+
* In the PATCH request line, replace `{id}` with `1`.
525+
526+
* Add the following lines immediately after the PATCH request line:
527+
528+
```
529+
Content-Type: application/json
530+
531+
{
532+
"name": "run errands"
533+
}
534+
```
535+
536+
The preceding code adds a Content-Type header and a JSON request body with only the field to update.
537+
538+
* Select the **Send request** link that is above the new PATCH request line.
539+
540+
The PATCH request is sent to the app and the response is displayed in the **Response** pane. The response body is empty, and the status code is 204.
541+
542+
# [Visual Studio Code](#tab/visual-studio-code)
543+
544+
Use Swagger to send a PATCH request:
545+
546+
* Select **Patch /todoitems/{id}** > **Try it out**.
547+
548+
* Set the **id** field to `1`.
549+
550+
* Set the request body to the following JSON:
551+
552+
```json
553+
{
554+
"name": "run errands"
555+
}
556+
```
557+
558+
* Select **Execute**.
559+
560+
---
561+
494562
## Examine and test the DELETE endpoint
495563

496564
The sample app implements a single DELETE endpoint using `MapDelete`:

aspnetcore/tutorials/min-web-api/samples/7.x/todo/Program.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,22 @@ is Todo todo
6161
});
6262
// </snippet_put>
6363

64+
// <snippet_patch>
65+
app.MapPatch("/todoitems/{id}", async (int id, Todo inputTodo, TodoDb db) =>
66+
{
67+
var todo = await db.Todos.FindAsync(id);
68+
69+
if (todo is null) return Results.NotFound();
70+
71+
if (inputTodo.Name is not null) todo.Name = inputTodo.Name;
72+
if (inputTodo.IsComplete != todo.IsComplete) todo.IsComplete = inputTodo.IsComplete;
73+
74+
await db.SaveChangesAsync();
75+
76+
return Results.NoContent();
77+
});
78+
// </snippet_patch>
79+
6480
// <snippet_delete>
6581
app.MapDelete("/todoitems/{id}", async (int id, TodoDb db) =>
6682
{

aspnetcore/tutorials/min-web-api/samples/8.x/todo/Program.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,22 @@ is Todo todo
6161
});
6262
// </snippet_put>
6363

64+
// <snippet_patch>
65+
app.MapPatch("/todoitems/{id}", async (int id, Todo inputTodo, TodoDb db) =>
66+
{
67+
var todo = await db.Todos.FindAsync(id);
68+
69+
if (todo is null) return Results.NotFound();
70+
71+
if (inputTodo.Name is not null) todo.Name = inputTodo.Name;
72+
if (inputTodo.IsComplete != todo.IsComplete) todo.IsComplete = inputTodo.IsComplete;
73+
74+
await db.SaveChangesAsync();
75+
76+
return Results.NoContent();
77+
});
78+
// </snippet_patch>
79+
6480
// <snippet_delete>
6581
app.MapDelete("/todoitems/{id}", async (int id, TodoDb db) =>
6682
{

aspnetcore/tutorials/min-web-api/samples/9.x/todo/Program.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,22 @@ is Todo todo
6161
});
6262
// </snippet_put>
6363

64+
// <snippet_patch>
65+
app.MapPatch("/todoitems/{id}", async (int id, Todo inputTodo, TodoDb db) =>
66+
{
67+
var todo = await db.Todos.FindAsync(id);
68+
69+
if (todo is null) return Results.NotFound();
70+
71+
if (inputTodo.Name is not null) todo.Name = inputTodo.Name;
72+
if (inputTodo.IsComplete != todo.IsComplete) todo.IsComplete = inputTodo.IsComplete;
73+
74+
await db.SaveChangesAsync();
75+
76+
return Results.NoContent();
77+
});
78+
// </snippet_patch>
79+
6480
// <snippet_delete>
6581
app.MapDelete("/todoitems/{id}", async (int id, TodoDb db) =>
6682
{

0 commit comments

Comments
 (0)