fix min web API todo /2 (#23820)

* fix min web API todo /2

* fix min web API todo /2
pull/23821/head
Rick Anderson 2021-11-09 07:06:43 -10:00 committed by GitHub
parent dc0bb05c3e
commit 243d9a82dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 4 additions and 3 deletions

View File

@ -419,7 +419,7 @@ A DTO may be used to:
To demonstrate the DTO approach, update the `Todo` class to include a secret field:
[!code-csharp[](min-web-api/samples/6.x/todoDTO/Program.cs?name=snippet_secret&highlight=7)]
[!code-csharp[](min-web-api/samples/6.x/todoDTO/Program.cs?name=snippet_secret&highlight=6)]
The secret field needs to be hidden from this app, but an administrative app could choose to expose it.

View File

@ -28,7 +28,7 @@ app.MapPost("/todoitems", async (TodoItemDTO todoItemDTO, TodoDb db) =>
db.Todos.Add(todoItem);
await db.SaveChangesAsync();
return Results.Created($"/todoitems/{todoItem.Id}", todoItem);
return Results.Created($"/todoitems/{todoItem.Id}", new TodoItemDTO(todoItem));
});
#region snippet_put
@ -53,7 +53,7 @@ app.MapDelete("/todoitems/{id}", async (int id, TodoDb db) =>
{
db.Todos.Remove(todo);
await db.SaveChangesAsync();
return Results.Ok(todo);
return Results.Ok(new TodoItemDTO(todo));
}
return Results.NotFound();
@ -74,6 +74,7 @@ public class Todo
#region snippet_DTO
public class TodoItemDTO
{
public int Id { get; set; }
public string? Name { get; set; }
public bool IsComplete { get; set; }