liveBook · Manning

Api model

As explained in ASP.NET Core in Action, 3e

An API model is the data representation returned by an API action for serialization and transmission to a client.

An API model is the data returned by an API action for serialization and transmission to a client. Unlike a view model or PageModel, it normally contains response data rather than metadata describing how to generate a response. section 20.2 section 20.7

The action creates the API model after application logic supplies the required data. An output formatter then serializes it into a machine-readable representation such as JSON or XML, and the response continues through the middleware pipeline to the caller. section 20.2 section 20.0

API actions can shape their result in two common ways:

  • Return data directly when the action has a straightforward response. section 20.7
  • Return ActionResult<T> when the action may produce more than one result type, such as data or an error response. section 20.7

Returning fruit data

An action handling an apples category can receive fruit objects from the application model, place that data in an API model, and return it. The output formatter can serialize the result as JSON; content negotiation can select another supported format, such as XML, when the client requests it. section 20.2 section 20.0

Figure 20.6 A call to a web API endpoint in an e-commerce ASP.NET Core web application. The ghosted portion of the diagram is identical to figure 20.5.
CH20_F06_Lock3

Use the request-flow diagram to follow how an API endpoint transforms application data into a response for its client. section 20.2

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains creation, serialization, format selection, and result typing, but does not detail model class design.
Examples
Uses fruit objects from an apples category and JSON or XML serialization as concrete response examples.
What it assumes
Builds on MVC, application models, model binding, validation, and middleware-based request handling.
What you can do after
Apply API models with output formatters to return client-readable JSON or XML from controller actions.
How it connects
Connects API models with MVC, output formatters, content negotiation, PageModels, view models, and ActionResult<T>.

Also explained in

Compare how 3 books explain api model →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.