liveBook · Manning

Deletion

As explained in Building Web APIs with ASP.NET Core

DELETE is an HTTP request method for removing a resource.

HTTP DELETE requests removal of a resource. In a board-game API, an endpoint can accept the game’s ID, find the record, delete it if present, save the change, and return a response DTO. Repeating the request for the same ID should have the same effect as making it once. section 5.5.2 section 1.1.4

A deletion endpoint can:

  • Use the HttpDelete attribute and accept an integer ID. section 5.5.2
  • Look up the record; if found, remove it through EF Core and save the changes. section 5.5.2
  • Return a RestDTO containing the found record, or null if no record was found. section 5.5.2
  • Treat deletion as a privileged operation: it can permanently change stored data, so authorization matters. section 5.5
Listing 5.11 BoardGameController’s Delete method
[HttpDelete(Name = "DeleteBoardGame")]
[ResponseCache(NoStore = true)]
public async Task<RestDTO<BoardGame?>> Delete(int id)
{
   var boardgame = await _context.BoardGames
       .Where(b => b.Id == id)
       .FirstOrDefaultAsync();
   if (boardgame != null)
   {
       _context.BoardGames.Remove(boardgame);
       await _context.SaveChangesAsync();
   };

   return new RestDTO<BoardGame?>()
   {
       Data = boardgame,
       Links = new List<LinkDTO>
       {
           new LinkDTO(
                   Url.Action(
                       null,
                       "BoardGames",
                       id,
                       Request.Scheme)!,
                   "self",
                   "DELETE"),
       }
   };
}

See the BoardGameController implementation, including its lookup, removal, save, and response-link logic. section 5.5.2

Verify deletion

In Swagger UI, execute DELETE /BoardGames with ID 181 to delete the Risk board game. The described test checks for an HTTP 200 response containing the targeted game’s information. section 5.5.2

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Implements lookup, removal, persistence, and response construction while deferring authentication and authorization.
Examples
Tests deleting the Risk board game with ID 181 through Swagger UI.
What it assumes
Assumes familiarity with basic database manipulation but requires no specific SQL knowledge.
What you can do after
Apply EF Core and HttpDelete to implement and test a resource-deletion endpoint.
How it connects
Connects HTTP methods, REST, CRUD, EF Core, and data-transfer objects.

Also explained in

Compare how 8 books explain deletion →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Web APIs with ASP.NET Core. The section links point to the parts of the book it draws on.