liveBook · Manning

Web API action method

As explained in Building Web APIs with ASP.NET Core

An action method is a controller method that handles an HTTP request and produces an HTTP response.

An action method is a controller method that handles an HTTP request and produces an HTTP response. Related actions live in a controller, where shared routing, services, authorization, caching, and filters can be configured once. Web APIs commonly inherit from ControllerBase and return data such as JSON or XML rather than views. section 1.2.3

Create a controller, add routing and HTTP-method attributes, then implement the method that returns the required result. In a board-game API, BoardGamesController uses [Route("[controller]")], [ApiController], and [HttpGet(Name = "GetBoardGames")] to expose /BoardGames and return an array of BoardGame objects. section 2.4.5

Important conventions include:

  • Use [HttpGet], [HttpDelete], or another HTTP attribute to constrain which request method reaches an action. section 1.2.3 section 3.4.3
  • Add route templates such as [HttpGet("{id}")] when an action receives a value from the URL. section 1.2.3
  • Return framework results such as ContentResult when the endpoint must send response text, and preserve required CORS and caching settings. section 3.4.3
  • Use controller boundaries to group related endpoints; OpenAPI tooling can then group their actions under controller-based tags. section 11.2.7
  • Use an action as a wrapper when it delegates work to another client, such as a gRPC client, and apply authorization attributes to restricted operations. section 10.5.4
[ApiController]                                #1
[Route("api/[controller]")]                    #2
public class SampleController : ControllerBase
{
    public SampleController()
    {
    }
 
    [HttpGet]                                  #3
    public string Get()
    {
        return "TODO: return all items";
    }
 
    [HttpGet("{id}")]                          #4
    public string Get(int id)
    {
        return $"TODO: return the item with id #{id}";
    }
 
    [HttpDelete("{id}")]                       #5
    public string Delete(int id)
    {
        return $"TODO: delete the item with id #{id}";
    }
}

Use this compact controller example to inspect route prefixes, HTTP-method attributes, identifier parameters, and ControllerBase inheritance together. section 1.2.3

Board-game collection endpoint

A GET request to /BoardGames can return three BoardGame objects—Axis & Allies, Citadels, and Terraforming Mars—with their identifiers, names, and release years. The same behavior could be implemented with a Minimal API, but a controller remains convenient as data retrieval and updates become more complex. section 2.4.5

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Explains attributes, routing, return values, grouping, and delegation, but stops short of detailed request-pipeline internals.
Examples
Uses board-game listing, identifier lookup, deletion, text responses, optional query parameters, and gRPC wrapper actions.
What it assumes
Assumes basic C# and ASP.NET Core project familiarity while introducing controller conventions through concrete API endpoints.
What you can do after
Build it from scratch by creating controllers, applying route and HTTP attributes, returning data, and configuring endpoint behavior.
How it connects
Connects controllers with routing, ControllerBase, Minimal APIs, OpenAPI, CORS, caching, authorization, and gRPC clients.

Also explained in

Compare how 3 books explain web API action method →

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.