liveBook · Manning

Hypermedia

As explained in Building Web APIs with ASP.NET Core

Hypermedia is media content that embeds links or controls enabling navigation to related resources or actions.

Hypermedia lets a response tell a client what it can do next. In a RESTful API, this is the HATEOAS constraint: clients follow descriptive links supplied by the server instead of needing to know every resource URI in advance. That separation lets server functionality evolve with less dependence on client-side knowledge. section 1.1.4

A board-game list that returns only game objects leaves clients to discover related actions elsewhere. To adopt a uniform interface, return the requested data alongside links that describe available requests. Links for changing resources should reflect what the client is permitted to do. section 3.1.6

One ASP.NET Core implementation uses two response types:

  • LinkDTO holds an href, a relationship (rel), and a type. In the example, the type is the HTTP method GET. section 3.1.6
  • RestDTO<T> wraps the requested Data and a list of Links. The controller's Get() action returns RestDTO<BoardGame[]> and creates a “self” link using Url.Action and the request scheme. section 3.1.6
public RestDTO<BoardGame[]> Get()           #1
{
    return new RestDTO<BoardGame[]>()       #2
    {
        Data = new BoardGame[] {
            new BoardGame() {
                Id = 1,
                Name = "Axis & Allies",
                Year = 1981
            },
            new BoardGame() {
                Id = 2,
                Name = "Citadels",
                Year = 2000
            },
            new BoardGame() {
                Id = 3,
                Name = "Terraforming Mars",
                Year = 2016
            }
        },
        Links = new List<LinkDTO> {      #3
            new LinkDTO(
                Url.Action(null, "BoardGames", null, Request.Scheme)!,
                "self",
                "GET"),
        }
    };
}

The controller listing shows where the board-game array and its generated “self” link enter the same response. section 3.1.6

Check the response

Request the BoardGames endpoint and inspect the JSON. It contains a data array with three games and a links array whose “self” entry gives an href and GET method. This first link demonstrates the response shape; it does not yet show links for modifying games. section 3.1.6

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Works through response DTOs and one generated self link, while leaving modification links as a proposed extension.
Examples
Shows a BoardGames controller returning three games with a generated GET self link in JSON.
What it assumes
Assumes a working minimal ASP.NET Core web API and introduces the DTO structure needed to add links.
What you can do after
Build it from scratch using C# response DTOs and an ASP.NET Core controller that returns data with descriptive links.
How it connects
Connects HATEOAS to REST, the uniform interface, resource representations, HTTP methods, and DTOs.

Also explained in

Compare how 3 books explain hypermedia →

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.