HATEOAS in .NET

Osama HaiDer
3 min readDec 12, 2024

--

What is HATEOAS?

HATEOAS stands for Hypermedia as the Engine of Application State. It’s a key concept of REST (Representational State Transfer) that allows a client to navigate an API dynamically using hyperlinks, without requiring hardcoded knowledge of the API structure.

In simpler terms, HATEOAS means your API provides extra links in its responses. These links tell clients what actions they can take next, making your API easier to use and self-explanatory.

Why is HATEOAS Important?

Without HATEOAS, clients need to know how to build URLs or endpoints to perform actions. With HATEOAS, the API itself tells the client what is possible. This helps:

  1. Ease of Use: Clients don’t need to guess or hardcode URLs.
  2. Flexibility: APIs can evolve without breaking clients.
  3. Discoverability: Clients can explore available actions dynamically.

Example Scenario

Let’s consider a simple .NET API for managing books in a library.

Without HATEOAS

When a client requests a book’s details, the API might return:

{
"id": 1,
"title": "C# Basics",
"author": "John Doe"
}

But what if the client wants to update or delete this book? The client must know the exact URLs for those actions beforehand, which is not ideal.

With HATEOAS

When HATEOAS is implemented, the response might look like this:

{
"id": 1,
"title": "C# Basics",
"author": "John Doe",
"links": [
{ "rel": "self", "href": "/api/books/1", "method": "GET" },
{ "rel": "update", "href": "/api/books/1", "method": "PUT" },
{ "rel": "delete", "href": "/api/books/1", "method": "DELETE" }
]
}

Here, the links array shows what the client can do with this book (e.g., view, update, or delete).

Implementing HATEOAS in .NET

Here’s how you can add HATEOAS to your .NET API:

  1. Create a DTO (Data Transfer Object) with Links
public class BookDto
{
public int Id { get; set; }
public string Title { get; set; }
public string Author { get; set; }
public List<Link> Links { get; set; } = new List<Link>();
}

public class Link
{
public string Rel { get; set; }
public string Href { get; set; }
public string Method { get; set; }
}

2. Add Links in Your Controller

[ApiController]
[Route("api/books")]
public class BooksController : ControllerBase
{
[HttpGet("{id}")]
public IActionResult GetBook(int id)
{
var book = new BookDto
{
Id = id,
Title = "C# Basics",
Author = "John Doe"
};

// Add HATEOAS links
book.Links.Add(new Link { Rel = "self", Href = Url.Action("GetBook", new { id }), Method = "GET" });
book.Links.Add(new Link { Rel = "update", Href = Url.Action("UpdateBook", new { id }), Method = "PUT" });
book.Links.Add(new Link { Rel = "delete", Href = Url.Action("DeleteBook", new { id }), Method = "DELETE" });

return Ok(book);
}

[HttpPut("{id}")]
public IActionResult UpdateBook(int id, [FromBody] BookDto book)
{
return Ok($"Book {id} updated successfully.");
}

[HttpDelete("{id}")]
public IActionResult DeleteBook(int id)
{
return Ok($"Book {id} deleted successfully.");
}
}

Testing the API

  1. Make a GET request to /api/books/1.
  2. You’ll see the book’s details along with HATEOAS links.
  3. Use the provided links to perform actions like update or delete.

Benefits of HATEOAS in .NET

  • Dynamic Navigation: Clients can discover actions directly from the API response.
  • Reduced Documentation Dependency: Clients don’t need extensive documentation to know the next steps.
  • Future-Proofing: If URLs change, clients remain unaffected because they rely on links from the API.

Conclusion

HATEOAS makes APIs smarter and more user-friendly by providing clients with the next possible actions as links. In .NET, implementing HATEOAS is straightforward and improves the overall experience of consuming APIs.

Use HATEOAS in your APIs to make them flexible, discoverable, and future-proof.

--

--

Osama HaiDer
Osama HaiDer

Written by Osama HaiDer

SSE at TEO International | .Net | Azure | AWS | Web APIs | C#

No responses yet