Skip to main content

API Design in ASP.NET Core Part II

This week I had the honor to give a training to some of the newly started young professionals in our organisation. The topic of the training was API design in ASP.NET Core. During this training we discussed multiple topics and a lot of interesting questions were raised. I'll try to tackle some of them with a blog post.

The question I try to tackle today is...

When do I know that my Web API is truly RESTful?

Yesterday I introduced the concept of REST and mentioned that it was an architecture style. As a result you’ll find a lot of different flavors of Web API’s in the wild each using a different approach and all calling themselves REST API’s.

To help you answer the question above, we can use the Richardson Maturity Model.

Leonard Richardson analyzed a hundred different web service designs and divided these designs into four categories. These categories are based on how much the web services are REST compliant.

He used three main factors to decide the maturity of a service. These factors are

  • Correct usage of URI’s
  • Correct usage of HTTP methods
  • Usage of HATEOAS (Hypermedia)

Let’s walk through the different levels:

LEVEL 0: POX swamp

Level 0 is also often referred to as POX (Plain Old XML). At level 0, HTTP is used only as a transport protocol. For zero maturity level services, we use a single URL and a single HTTP method. We send a request to the same URI for obtaining and posting the data.  All operations are performed using the POST method. 

LEVEL 1: Multiple URI based resource and single verb

In level 1 , each resource is mapped to a specific URI. However, only one HTTP method (POST) is used for retrieving and creating data. For example, we need to access the courses from a specific author.

LEVEL 2: Multiple URI based resource and HTTP verbs

At Level 2 requests are sent with the correct HTTP verb. A correct HTTP response code is returned for each request.

For example: To get the courses from an author, we send a request with the URI
/course">/course">http://localhost:8080/authors/<author-id>/courses and the server sends proper response 200 OK.

LEVEL 3: HATEOS

Level 3 is the highest. It combines level 2 and HATEOS. It is helpful in self-documentation. HATEOS guides where other resources can be found.

A HATEOS response when getting a course from an author, could look like this:

HTTP/1.1 200 OK

{
    "course": {
        "title": "API Design in ASP.NET Core",
        "description": "API Design in ASP.NET Core",
        "links": {
"_self": "/authors/12345/courses/678", "author": "/authors/12345", "subscriptions": "/authors/12345/courses/678/subscriptions", "videos": "/authors/12345/courses/678/videos", "exercises": "/authors/12345/courses/678/exercises" } } }

IMPORTANT: According to Roy Fielding, we can consider an API to be RESTful only when it reaches level 4. The other levels are only stepping stones to becoming one.

Popular posts from this blog

DevToys–A swiss army knife for developers

As a developer there are a lot of small tasks you need to do as part of your coding, debugging and testing activities.  DevToys is an offline windows app that tries to help you with these tasks. Instead of using different websites you get a fully offline experience offering help for a large list of tasks. Many tools are available. Here is the current list: Converters JSON <> YAML Timestamp Number Base Cron Parser Encoders / Decoders HTML URL Base64 Text & Image GZip JWT Decoder Formatters JSON SQL XML Generators Hash (MD5, SHA1, SHA256, SHA512) UUID 1 and 4 Lorem Ipsum Checksum Text Escape / Unescape Inspector & Case Converter Regex Tester Text Comparer XML Validator Markdown Preview Graphic Color B

Help! I accidently enabled HSTS–on localhost

I ran into an issue after accidently enabling HSTS for a website on localhost. This was not an issue for the original website that was running in IIS and had a certificate configured. But when I tried to run an Angular app a little bit later on http://localhost:4200 the browser redirected me immediately to https://localhost . Whoops! That was not what I wanted in this case. To fix it, you need to go the network settings of your browser, there are available at: chrome://net-internals/#hsts edge://net-internals/#hsts brave://net-internals/#hsts Enter ‘localhost’ in the domain textbox under the Delete domain security policies section and hit Delete . That should do the trick…

Azure DevOps/ GitHub emoji

I’m really bad at remembering emoji’s. So here is cheat sheet with all emoji’s that can be used in tools that support the github emoji markdown markup: All credits go to rcaviers who created this list.