Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Building Beautiful REST APIs in ASP.NET Core

Building Beautiful REST APIs in ASP.NET Core

Learn how to use ASP.NET Core to build beautiful RESTful APIs in this presentation by Nate Barbettini.

Nate Barbettini

December 15, 2016
Tweet

More Decks by Nate Barbettini

Other Decks in Programming

Transcript

  1. About me  Developer Advocate @ Okta  Microsoft MVP

     Blog @ www.recaffeinate.co  Tweet @nbarbettini
  2. What is REST? REST != returning JSON over HTTP REST

    = an abstraction with specific rules REST = all about resources and links
  3. 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
  4. 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
  5. 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 <html> <a href="en.wikipedia.org">English</a> <a href="es.wikipedia.org">Español</a> <a href="de.wikipedia.org">Deutsch</a> <form action="/login" method="POST"> <input name="username"> <input type="password" name="password"> </form> </html>
  6. The Ion hypermedia type  Plain ol’ JSON  Resources

    and collections  Links and HATEOAS
  7. 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
  8. 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
  9. 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
  10. 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