liveBook · Manning

Request message

As explained in Building Web APIs with ASP.NET Core

POST is an HTTP request method for creating a resource or causing a state change.

POST is an HTTP request method for creating a resource or causing a state change. In REST, it commonly handles operations whose result is not guaranteed to repeat identically. section 1.1.4 section 5.5.1

Choose POST for an update when each execution can produce a distinct server effect. The board-game API updates LastModifiedDate on every save, so repeating the operation is not idempotent; PUT would suit a guaranteed identical result instead. section 5.5.1

A practical POST-based update flow:

  • Accept client data through a BoardGameDTO rather than exposing the database entity directly. section 5.5.1
  • Find the board game using the submitted identifier and apply valid optional fields such as Name and Year. section 5.5.1
  • Set LastModifiedDate, update the entity, save changes asynchronously, and return the resulting data. section 5.5.1
Listing 5.10 BoardGameController’s Post method
[HttpPost(Name = "UpdateBoardGame")]
[ResponseCache(NoStore = true)]
public async Task<RestDTO<BoardGame?>> Post(BoardGameDTO model)
{
   var boardgame = await _context.BoardGames
       .Where(b => b.Id == model.Id)
       .FirstOrDefaultAsync();
   if (boardgame != null)
   {
       if (!string.IsNullOrEmpty(model.Name))
           boardgame.Name = model.Name;
       if (model.Year.HasValue && model.Year.Value > 0)
           boardgame.Year = model.Year.Value;
       boardgame.LastModifiedDate = DateTime.Now;
       _context.BoardGames.Update(boardgame);
       await _context.SaveChangesAsync();
   };

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

Inspect the controller method to follow the POST update sequence from lookup through persistence and response construction. section 5.5.1

Updating a board game

A POST request for board game ID 181 can change the name from “Risk!” to “Risk” and the year from 1980 to 1959. The controller then records the modification and saves the updated entity. section 5.5.1

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Explains POST semantics, idempotence-based method selection, DTO input, validation, persistence, and response construction.
Examples
Uses a BoardGameController endpoint that changes board game ID 181 from Risk! to Risk and from 1980 to 1959.
What you can do after
Apply POST by implementing a DTO-backed controller action that updates a record, persists changes, and returns the result.
How it connects
Connects POST with REST, HTTP PUT, idempotence, DTOs, entity classes, and CRUD operations.

Also explained in

Compare how 14 books explain request message →

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.