From 3d79a4b54567114895dc0716c93cddbebbba2911 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A9ter=20Bozs=C3=B3?= Date: Mon, 7 Aug 2017 00:15:25 +0200 Subject: [PATCH] Fix DELETE operation in TodoController (#3898) By using First(), there's the opportunity for an uncaught exception when the endpoint is called with an ID not present in the database. --- .../first-web-api/sample/TodoApi/Controllers/TodoController.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aspnetcore/tutorials/first-web-api/sample/TodoApi/Controllers/TodoController.cs b/aspnetcore/tutorials/first-web-api/sample/TodoApi/Controllers/TodoController.cs index 14c276a6de..27287400af 100644 --- a/aspnetcore/tutorials/first-web-api/sample/TodoApi/Controllers/TodoController.cs +++ b/aspnetcore/tutorials/first-web-api/sample/TodoApi/Controllers/TodoController.cs @@ -85,7 +85,7 @@ namespace TodoApi.Controllers [HttpDelete("{id}")] public IActionResult Delete(long id) { - var todo = _context.TodoItems.First(t => t.Id == id); + var todo = _context.TodoItems.FirstOrDefault(t => t.Id == id); if (todo == null) { return NotFound();