DELETE
DELETE removes the item at an address. It sends no body; the action deletes by the id from the route and answers 204 No Content, or 404 Not Found when there was nothing to delete.
Journeyman · ASP.NET Core · REST API · Updated
After this lesson you can
- Write a DELETE action that removes an item and answers 204 No Content, or 404 Not Found when it isn't there
Key takeaways
- A
DELETEto an item's address removes it. It sends no body: the id comes from the route. - The DAO returns
rows == 1, and the action answers204 No Content, or404 Not Foundwhen there was nothing to delete. - Deleting a category that still has products breaks a foreign key, and the uncaught
SqlExceptionsends the client a500with the stack trace. - Errors like that are best handled once, for the whole API, not in each action.
A client deletes an item with a DELETE to that item's address. Like a GET, it has no body: the address says which item.
The DAO
public bool Delete(int categoryId)
{
using SqlConnection connection = new SqlConnection(connectionString);
int rows = connection.Execute("DELETE FROM Categories WHERE CategoryId = @CategoryId;", new { CategoryId = categoryId });
return rows == 1;
}
Dapper's Execute returns how many rows the statement changed, so the number of rows tells you whether there was anything to delete. Unlike Update, there's nothing to hand back: the row is gone.
The action
[HttpDelete("{id}")]
public IActionResult Delete(int id)
{
if (!categoryDao.Delete(id))
{
return NotFound();
}
return NoContent();
}
Restart the API, and send Delete Snacks from your Insomnia collection again. The 405 Method Not Allowed from Insomnia: DELETE is now:
HTTP/1.1 204 No Content
Send the same request again, and it answers 404 Not Found: category 9 is already gone. Category 9 answers 404 too.
When the database says no
Now send Delete Beverages, a category that has products:
DELETE {{ _.base_url }}/api/categories/1
HTTP/1.1 500 Internal Server Error
Microsoft.Data.SqlClient.SqlException (0x80131904): The DELETE statement conflicted with the REFERENCE constraint "FK_Products_Categories". ...
Beverages has products, and the foreign key from Database Design in SQL Server won't let its category go. The DAO throws a SqlException, nobody catches it, and the client gets a 500 with the full error and stack trace.
That's fine while you develop: you see exactly what broke. But a real client should never see your stack trace, and a foreign-key conflict isn't really a server crash. Handling Global Exceptions in ASP.NET Core shows how to answer with something better, in one place for the whole API.
The full controller
With GET, POST, PUT and DELETE, the categories API is complete: five endpoints, all CRUD, in one class. Your Northwind API collection in Insomnia holds a request for every one of them, and every to-do item now answers what you wrote down, except Delete Beverages, which Handling Global Exceptions in ASP.NET Core puts right.
| Request | Action | Answers |
|---|---|---|
GET /api/categories |
GetAll() |
200 with the list |
GET /api/categories/8 |
GetById(8) |
200 with the category |
POST /api/categories |
Add(category) |
201 with the new category and its address |
PUT /api/categories/8 |
Update(8, category) |
200 with the updated category, or 404 |
DELETE /api/categories/8 |
Delete(8) |
204, or 404 |
Do it: delete a category and choose the answer
The DELETE action asks the DAO to delete, then answers 204 No Content, or 404 Not Found when there was nothing to delete. Write both steps as one method, for a list of categories instead of a database.
The method gets two values:
categories: the saved categories, aList<Category>. EachCategoryhas aCategoryIdand aCategoryName.id: the id from the address, anint, such as9fromDELETE /api/categories/9.
For example, the list holds Beverages (1), Seafood (8) and Snacks (9), and a client sends DELETE /api/categories/9:
| Step | Here |
|---|---|
Remove the category whose CategoryId is 9 |
1 removed: Snacks |
| One was removed, so the answer is | 204 |
| The list now holds | Beverages and Seafood |
So Delete(categories, 9) returns 204.
Your job: write the method Delete(List<Category> categories, int id) so it returns the status code, an int.
- Remove the category whose
CategoryIdisidfrom the list. - When one was removed, return
204. - When none was removed (no category has that id), return
404. The list stays as it was.
Practiced live on the lesson page: write the C# right there and check it.
Check yourself
1 question to try on the lesson page.
So what
DELETE is the simplest action: an id in, a status out. The interesting part is what happens when the database refuses, and that's a job for the whole API, not one action.