Skip to content

Commit 3c363a6

Browse files
CopilotwadepickettCopilot
authored
Clarify scaffolded controller flow and PostTodoItem step in controller-based Web API tutorial (#37526)
* Initial plan * Clarify scaffolded controller and examine PostTodoItem in Web API tutorial Co-authored-by: wadepickett <10985336+wadepickett@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: Wade Pickett <wpickett@microsoft.com> * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: wadepickett <10985336+wadepickett@users.noreply.github.com> Co-authored-by: Wade Pickett <wpickett@microsoft.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent 910d8e5 commit 3c363a6

3 files changed

Lines changed: 41 additions & 10 deletions

File tree

aspnetcore/tutorials/first-web-api.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
---
22
title: "Tutorial: Create a controller-based web API with ASP.NET Core"
3+
ai-usage: ai-assisted
34
author: wadepickett
45
description: Learn how to build a controller-based web API with ASP.NET Core.
56
ms.author: wpickett
6-
ms.date: 09/15/2025
7+
ms.date: 08/24/2026
78
uid: tutorials/first-web-api
89
---
910

@@ -408,9 +409,19 @@ The ASP.NET Core templates for:
408409

409410
When the `[action]` token isn't in the route template, the [action](xref:mvc/controllers/routing#action) name (method name) isn't included in the endpoint. That is, the action's associated method name isn't used in the matching route.
410411

411-
## Update the PostTodoItem create method
412+
Open the generated `Controllers/TodoItemsController.cs` file to review the scaffolded code. Scaffolding generates a complete, working controller with action methods that create, read, update, and delete `TodoItem` data:
412413

413-
Update the return statement in the `PostTodoItem` to use the [nameof](/dotnet/csharp/language-reference/operators/nameof) operator:
414+
* `GetTodoItems`: Get all to-do items.
415+
* `GetTodoItem`: Get a to-do item by ID.
416+
* `PostTodoItem`: Create a to-do item.
417+
* `PutTodoItem`: Update an existing to-do item.
418+
* `DeleteTodoItem`: Delete a to-do item.
419+
420+
The following sections examine these generated methods, starting with `PostTodoItem`.
421+
422+
## Examine the `PostTodoItem` create method
423+
424+
Examine the scaffolded `PostTodoItem` method. The generated code already works. The return statement uses the [nameof](/dotnet/csharp/language-reference/operators/nameof) operator so the action name isn't hard-coded as a string. The commented-out line shows the equivalent hard-coded form that `nameof` replaces:
414425

415426
:::code language="csharp" source="~/tutorials/first-web-api/samples/9.0/TodoApi/Controllers/TodoItemsController.cs" id="snippet_Create":::
416427

@@ -422,7 +433,7 @@ The <xref:Microsoft.AspNetCore.Mvc.ControllerBase.CreatedAtAction%2A> method:
422433

423434
* Returns an [HTTP 201 status code](https://developer.mozilla.org/docs/Web/HTTP/Status/201) if successful. `HTTP 201` is the standard response for an `HTTP POST` method that creates a new resource on the server.
424435
* Adds a [Location](https://developer.mozilla.org/docs/Web/HTTP/Headers/Location) header to the response. The `Location` header specifies the [URI](https://developer.mozilla.org/docs/Glossary/URI) of the newly created to-do item. For more information, see [10.2.2 201 Created](https://www.rfc-editor.org/rfc/rfc9110.html#section-10.2.2).
425-
* References the `GetTodoItem` action to create the `Location` header's URI. The C# `nameof` keyword is used to avoid hard-coding the action name in the `CreatedAtAction` call.
436+
* References the `GetTodoItem` action to create the `Location` header's URI.
426437

427438
<a name="post7"></a>
428439

aspnetcore/tutorials/first-web-api/includes/first-web-api7.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -338,9 +338,19 @@ The ASP.NET Core templates for:
338338

339339
When the `[action]` token isn't in the route template, the [action](xref:mvc/controllers/routing#action) name (method name) isn't included in the endpoint. That is, the action's associated method name isn't used in the matching route.
340340

341-
## Update the PostTodoItem create method
341+
Open the generated `Controllers/TodoItemsController.cs` file to review the scaffolded code. Scaffolding generates a complete, working controller with action methods that create, read, update, and delete `TodoItem` data:
342342

343-
Update the return statement in the `PostTodoItem` to use the [nameof](/dotnet/csharp/language-reference/operators/nameof) operator:
343+
* `GetTodoItems`: Get all to-do items.
344+
* `GetTodoItem`: Get a to-do item by ID.
345+
* `PostTodoItem`: Create a to-do item.
346+
* `PutTodoItem`: Update an existing to-do item.
347+
* `DeleteTodoItem`: Delete a to-do item.
348+
349+
The following sections examine these generated methods, starting with `PostTodoItem`.
350+
351+
## Examine the `PostTodoItem` create method
352+
353+
Examine the scaffolded `PostTodoItem` method, which already works as generated. The return statement uses the [nameof](/dotnet/csharp/language-reference/operators/nameof) operator so the action name isn't hard-coded as a string. The commented-out line shows the equivalent hard-coded form that `nameof` replaces:
344354

345355
[!code-csharp[](~/tutorials/first-web-api/samples/7.0/TodoApi/Controllers/TodoItemsController.cs?name=snippet_Create)]
346356

@@ -352,7 +362,7 @@ The <xref:Microsoft.AspNetCore.Mvc.ControllerBase.CreatedAtAction%2A> method:
352362

353363
* Returns an [HTTP 201 status code](https://developer.mozilla.org/docs/Web/HTTP/Status/201) if successful. `HTTP 201` is the standard response for an `HTTP POST` method that creates a new resource on the server.
354364
* Adds a [Location](https://developer.mozilla.org/docs/Web/HTTP/Headers/Location) header to the response. The `Location` header specifies the [URI](https://developer.mozilla.org/docs/Glossary/URI) of the newly created to-do item. For more information, see [10.2.2 201 Created](https://www.rfc-editor.org/rfc/rfc9110.html#section-10.2.2).
355-
* References the `GetTodoItem` action to create the `Location` header's URI. The C# `nameof` keyword is used to avoid hard-coding the action name in the `CreatedAtAction` call.
365+
* References the `GetTodoItem` action to create the `Location` header's URI.
356366

357367
<a name="post7"></a>
358368

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,9 +294,19 @@ The ASP.NET Core templates for:
294294

295295
When the `[action]` token isn't in the route template, the [action](xref:mvc/controllers/routing#action) name (method name) isn't included in the endpoint. That is, the action's associated method name isn't used in the matching route.
296296

297-
## Update the PostTodoItem create method
297+
Open the generated `Controllers/TodoItemsController.cs` file to review the scaffolded code. Scaffolding generates a complete, working controller with action methods that create, read, update, and delete `TodoItem` data:
298298

299-
Update the return statement in the `PostTodoItem` to use the [nameof](/dotnet/csharp/language-reference/operators/nameof) operator:
299+
* `GetTodoItems`: Get all to-do items.
300+
* `GetTodoItem`: Get a to-do item by ID.
301+
* `PostTodoItem`: Create a to-do item.
302+
* `PutTodoItem`: Update an existing to-do item.
303+
* `DeleteTodoItem`: Delete a to-do item.
304+
305+
The following sections examine these generated methods, starting with `PostTodoItem`.
306+
307+
## Examine the `PostTodoItem` create method
308+
309+
Examine the scaffolded `PostTodoItem` method. The generated code already works. The return statement uses the [nameof](/dotnet/csharp/language-reference/operators/nameof) operator so the action name isn't hard-coded as a string. The commented-out line shows the equivalent hard-coded form that `nameof` replaces:
300310

301311
[!code-csharp[](~/tutorials/first-web-api/samples/8.0/TodoApi/Controllers/TodoItemsController.cs?name=snippet_Create)]
302312

@@ -308,7 +318,7 @@ The <xref:Microsoft.AspNetCore.Mvc.ControllerBase.CreatedAtAction%2A> method:
308318

309319
* Returns an [HTTP 201 status code](https://developer.mozilla.org/docs/Web/HTTP/Status/201) if successful. `HTTP 201` is the standard response for an `HTTP POST` method that creates a new resource on the server.
310320
* Adds a [Location](https://developer.mozilla.org/docs/Web/HTTP/Headers/Location) header to the response. The `Location` header specifies the [URI](https://developer.mozilla.org/docs/Glossary/URI) of the newly created to-do item. For more information, see [10.2.2 201 Created](https://www.rfc-editor.org/rfc/rfc9110.html#section-10.2.2).
311-
* References the `GetTodoItem` action to create the `Location` header's URI. The C# `nameof` keyword is used to avoid hard-coding the action name in the `CreatedAtAction` call.
321+
* References the `GetTodoItem` action to create the `Location` header's URI.
312322

313323
<a name="post7"></a>
314324

0 commit comments

Comments
 (0)