Slide 1

Slide 1 text

Building beautiful REST APIs with ASP.NET Core Nate Barbettini @nbarbettini

Slide 2

Slide 2 text

About me  Developer Advocate @ Okta  Microsoft MVP  Blog @ www.recaffeinate.co  Tweet @nbarbettini

Slide 3

Slide 3 text

What is REST? REST != returning JSON over HTTP REST = an abstraction with specific rules REST = all about resources and links

Slide 4

Slide 4 text

RPC vs. REST REST (Representational State Transfer) • Endpoints are nouns (resources): api.example.io/users • Calling the endpoint acts on the resource: GET api.example.io/users/123 POST api.example.io/users/123 • Typically returns the resource that was acted upon RPC (Remote Procedure Call) • Endpoints are verbs (methods): api.example.io/getUser • Calling the endpoint calls the method: GET api.example.io/getUser POST api.example.io/updateUser • Returns the result of the function call

Slide 5

Slide 5 text

Bad RPC vs. Clean REST GET /getAllAccounts GET /getAccount?id=17 POST /createAccount POST /update?accountId=17 GET /findAccount?lastname=Skywalker GET /accounts GET /accounts/17 POST /accounts POST /accounts/17 GET /accounts?lastname=Skywalker

Slide 6

Slide 6 text

HATEOAS (hypermedia as the engine of application state)  Data exchange should be treated as if the client were a web browser  No documentation or out-of-band info needed GET wikipedia.org English Español Deutsch

Slide 7

Slide 7 text

REST + JSON is popular but it's really hard because there’s no spec.

Slide 8

Slide 8 text

The Ion hypermedia type  Plain ol’ JSON  Resources and collections  Links and HATEOAS

Slide 9

Slide 9 text

Ion objects (resources) { "title": "Who is the coolest Avenger?", "createdAt": "2017-07-24" } "href": "https://api.posts/conversations/123", GET https://api.posts/conversations/123

Slide 10

Slide 10 text

Ion links { "href": "https://api.posts/conversations/123", "title": "Who is the coolest Avenger?" "createdAt": "2017-07-24", "author": { "href": "https://api.posts/users/iron_man" }, "comments": { "href": "https://api.posts/conversations/123/comments", "rel": ["collection"] } } GET https://api.posts/conversations/123

Slide 11

Slide 11 text

Ion collections { "href": "https://api.posts/conversations/123/comments", "rel": ["collection"], "value": [ { "href": "http://api.posts/comments/456", "body": "Iron Man of course", "conversation": { "href": "/conversations/123" } }, { "href": "http://api.posts/comments/789", "body": "wait a sec...", "conversation": { "href": "/conversations/123" } } ] } GET https://api.posts/conversations/123/comments

Slide 12

Slide 12 text

Building on ASP.NET Core

Slide 13

Slide 13 text

Why ASP.NET Core  Fast  Clean  Modern  Familiar

Slide 14

Slide 14 text

Live coding

Slide 15

Slide 15 text

More resources  Example code https://github.com/nbarbettini/BeautifulRestApi  Video course http://bit.ly/restapicourse  Ion draft spec https://ionwg.org Thanks for your time! @nbarbettini https://www.recaffeinate.co