$30 off During Our Annual Pro Sale. View Details »

REST Easy - Building RESTful Web Services using ASP.Net Web API

neraath
August 25, 2012

REST Easy - Building RESTful Web Services using ASP.Net Web API

The epicenter of data sharing in "Web 2.0" are web services, but empowering browsers, devices, and other mashups to share data requires creating flexible, scalable, and easily consumable web services. SOAP and XMLRPC services are too heavy and rigid for both clients and servers. ASP.Net Web API is a new framework build on-top of ASP.Net MVC to help make implementing lightweight, RESTful services easy. This session will take you through the steps of designing a RESTful web service, then craft the web service in ASP.Net Web API.

neraath

August 25, 2012
Tweet

More Decks by neraath

Other Decks in Programming

Transcript

  1. REST Easy
    Building RESTful Services using ASP.Net Web API
    Saturday, August 25, 12

    View Slide

  2. Me
    • Fightin’ Texas Aggie
    • C# Developer since 2005
    • SharePoint and Identity
    Management Architect
    • Senior Consultant at
    Improving Enterprises
    [email protected]
    Saturday, August 25, 12

    View Slide

  3. Twitter Promotion
    Saturday, August 25, 12

    View Slide

  4. Twitter Promotion
    • @neraath
    Saturday, August 25, 12

    View Slide

  5. Twitter Promotion
    • @neraath
    • #houcodecamp
    Saturday, August 25, 12

    View Slide

  6. Twitter Promotion
    • @neraath
    • #houcodecamp
    • #rest
    Saturday, August 25, 12

    View Slide

  7. Twitter Promotion
    • @neraath
    • #houcodecamp
    • #rest
    • #thisguyisawesome
    Saturday, August 25, 12

    View Slide

  8. Twitter Promotion
    • @neraath
    • #houcodecamp
    • #rest
    • #thisguyisawesome
    • #bestspeakerever
    Saturday, August 25, 12

    View Slide

  9. Twitter Promotion
    • @neraath
    • #houcodecamp
    • #rest
    • #thisguyisawesome
    • #bestspeakerever
    • #horriblepresenter
    Saturday, August 25, 12

    View Slide

  10. Twitter Promotion
    • @neraath
    • #houcodecamp
    • #rest
    • #thisguyisawesome
    • #bestspeakerever
    • #horriblepresenter
    • #omgshootme
    Saturday, August 25, 12

    View Slide

  11. Agile, Microsoft, Open Technologies, UX
    Applied Training, Coaching, Mentoring
    Certified Consulting
    Rural Sourcing
    Recruiting Services
    Saturday, August 25, 12

    View Slide

  12. REST
    Saturday, August 25, 12

    View Slide

  13. Saturday, August 25, 12

    View Slide

  14. REpresentational State
    Transfer
    Saturday, August 25, 12

    View Slide

  15. Why REST?
    Saturday, August 25, 12

    View Slide

  16. Scalability
    Saturday, August 25, 12

    View Slide

  17. Architecture
    Independence
    Saturday, August 25, 12

    View Slide

  18. Modularity
    Saturday, August 25, 12

    View Slide

  19. Resource Addressing
    and Management
    Saturday, August 25, 12

    View Slide

  20. Self-Describing Messages
    Saturday, August 25, 12

    View Slide

  21. REST is NOT
    Saturday, August 25, 12

    View Slide

  22. Saturday, August 25, 12

    View Slide

  23. SOAP is Fugly






    IBM



    Saturday, August 25, 12

    View Slide

  24. REST’s Beauty is in
    HTTP Verbs and URIs
    Saturday, August 25, 12

    View Slide

  25. http://www.example.org/stock/price/IBM
    Saturday, August 25, 12

    View Slide

  26. GET
    GET /stock/price/IBM HTTP/1.1
    Host: www.example.org
    User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4)
    Accept-Encoding: gzip
    Accept-Charset: ISO-8859-1,UTF-8;q=0.7,*;q=0.7
    Cache-Control: no-cache
    Accept-Language: de,en;q=0.7,en-us;q=0.3
    Request
    date: Thu, 21 Jun 2012 21:55:57 GMT
    version: HTTP/1.1
    status: 200 OK
    content-length: 26550
    content-type: text/html; charset=UTF-8
    cache-control: private, max-age=0
    expires: -1
    Response
    Saturday, August 25, 12

    View Slide

  27. POST
    POST /stock/price HTTP/1.1
    Host: www.example.org
    Cache-Control: max-age=0
    User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4)
    Content-Type: application/x-www-form-urlencoded
    Accept: text/html,application/xhtml+xml;q=0.9,*/*;q=0.8
    id: IBM
    currentPrice:193.39
    delta:-5.39
    locale:en_US
    Request
    Date: Thu, 21 Jun 2012 21:55:57 GMT
    Version: HTTP/1.1
    Status: 201 CREATED
    Location: /stock/price/IBM
    Response
    Saturday, August 25, 12

    View Slide

  28. PUT
    PUT /stock/price/IBM HTTP/1.1
    Host: www.example.org
    Cache-Control: max-age=0
    User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4)
    Content-Type: application/x-www-form-urlencoded
    Accept: text/html,application/xhtml+xml;q=0.9,*/*;q=0.8
    currentPrice:193.39
    delta:-5.39
    locale:en_US
    Request
    Date: Thu, 21 Jun 2012 21:55:57 GMT
    Version: HTTP/1.1
    Status: 200 OK
    Response
    Saturday, August 25, 12

    View Slide

  29. DELETE
    DELETE /stock/price/IBM HTTP/1.1
    Host: www.example.org
    User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4)
    Accept-Encoding: gzip
    Accept-Charset: ISO-8859-1,UTF-8;q=0.7,*;q=0.7
    Cache-Control: no-cache
    Accept-Language: de,en;q=0.7,en-us;q=0.3
    Request
    date: Thu, 21 Jun 2012 21:55:57 GMT
    version: HTTP/1.1
    status: 204 NO CONTENT
    Response
    Saturday, August 25, 12

    View Slide

  30. Status Codes are Your
    Friend
    Saturday, August 25, 12

    View Slide

  31. 200 =
    Saturday, August 25, 12

    View Slide

  32. Saturday, August 25, 12

    View Slide

  33. 201 = Created
    Saturday, August 25, 12

    View Slide

  34. 204 = ?
    Saturday, August 25, 12

    View Slide

  35. Saturday, August 25, 12

    View Slide

  36. 301 = ?
    Saturday, August 25, 12

    View Slide

  37. Saturday, August 25, 12

    View Slide

  38. 304 =
    Saturday, August 25, 12

    View Slide

  39. Saturday, August 25, 12

    View Slide

  40. 400 =
    Saturday, August 25, 12

    View Slide

  41. Saturday, August 25, 12

    View Slide

  42. 401 = ?
    Saturday, August 25, 12

    View Slide

  43. Saturday, August 25, 12

    View Slide

  44. 403 =
    Saturday, August 25, 12

    View Slide

  45. Saturday, August 25, 12

    View Slide

  46. 404 = ?
    Saturday, August 25, 12

    View Slide

  47. Saturday, August 25, 12

    View Slide

  48. 500 =
    Saturday, August 25, 12

    View Slide

  49. Saturday, August 25, 12

    View Slide

  50. RESTful Services are
    Flexible
    Saturday, August 25, 12

    View Slide

  51. Content Negotiation
    GET /user/user001 HTTP/1.1
    Host: www.example.org
    Accept: application/json
    # Response - server supports JSON as well as XML
    HTTP/1.1 200 OK
    Content-Type: application/json
    {
    “user”: {
    “id”: “user001”,
    “link”: “http://example.org/user/user001”,
    “name”: “John Doe”,
    “email”: “[email protected]”,
    }
    }
    Saturday, August 25, 12

    View Slide

  52. Content Negotiation
    GET /user/user001 HTTP/1.1
    Host: www.example.org
    Accept: application/json
    # Response - server only supports XML
    HTTP/1.1 200 OK
    Content-Language: en-US
    Content-MD5: bbdc7bbb8ea5a689666e33ac922c0f83
    Content-Type: application/xml;charset=UTF-8

    user001

    John Doe
    [email protected]

    Saturday, August 25, 12

    View Slide

  53. Multiple Language
    Support
    # Response
    HTTP/1.1 200 OK
    Content-Language: kr

    강남구 삼성동 144-19,20 번지 JS 타워
    서울특별시
    135-090
    대한민국
    KR

    Saturday, August 25, 12

    View Slide

  54. Allow the Client to
    Request
    Accept: application/json;q=1.0, application=xml;q=0.6, *.*/q=0.0
    Accept-Language: fr;q=1.0, en;q=0.5
    Accept-Encoding: gzip
    Saturday, August 25, 12

    View Slide

  55. # Request
    GET /user/001/followers HTTP/1.1
    Accept: application/json,*/*;q=0.0
    # Response
    406 Not Acceptable
    Content-Type: application/json
    Link: ;rel="help"
    {
    "message" : "This server does not support JSON."
    }
    Saturday, August 25, 12

    View Slide

  56. Data Representations
    Saturday, August 25, 12

    View Slide

  57. REST Supports
    {
    “person”:
    {
    “name”: “Chris Weldon”,
    “email”: “[email protected]
    }
    }
    Saturday, August 25, 12

    View Slide

  58. REST Supports


    Chris Weldon
    [email protected]

    Saturday, August 25, 12

    View Slide

  59. REST Supports
    Your Own Representation Here
    Saturday, August 25, 12

    View Slide

  60. Authentication
    Saturday, August 25, 12

    View Slide

  61. HTTP Basic
    Authentication
    # Request
    GET /photos HTTP/1.1
    Host: www.example.org
    # Response
    401 Unauthorized
    WWW-Authenticate: Basic realm=”Photos App”
    Content-Type: application/xml;charset=UTF-8

    Unauthorized.

    Saturday, August 25, 12

    View Slide

  62. HTTP Basic
    Authentication
    # Request
    GET /photos HTTP/1.1
    Host: www.example.org
    Authorization: Basic cGhvdG9hcHAuMDAxOmJhc2ljYXV0aA==
    # Response
    HTTP/1.1 200 OK
    Content-Type: application/xml;charset=UTF-8
    ...
    Saturday, August 25, 12

    View Slide

  63. HTTP Digest
    Authentication
    # Request
    GET /photos HTTP/1.1
    Host: www.example.org
    # Response
    401 Unauthorized
    WWW-Authenticate: Digest realm=”Photos App”,
    nonce="6cf093043215da528d7b5039ed4694d3", qop=”auth”
    Content-Type: application/xml;charset=UTF-8

    Unauthorized.

    Saturday, August 25, 12

    View Slide

  64. HTTP Digest
    Authentication
    # Request
    GET /photos HTTP/1.1
    Host: www.example.org
    Authorization: Digest username=”photoapp.001”,
    realm=”Sample App”,
    nonce=”6cf093043215da528d7b5039ed4694d3”, uri=”/photos”,
    response=”f3ad23e0cfa012bcdd0912025689baea”, cnonce=”
    f3ad23e0cfa012bcdd0912025689baea”, nc=00000001, qop=”auth”
    # Response
    HTTP/1.1 200 OK
    Content-Type: application/xml;charset=UTF-8
    ...
    Saturday, August 25, 12

    View Slide

  65. Custom Authorization
    # Request
    GET /photos HTTP/1.1
    Host: www.example.org
    Authorization: AWS AWSAccessKeyId:Signature
    # Response
    HTTP/1.1 200 OK
    Content-Type: application/xml;charset=UTF-8
    ...
    Saturday, August 25, 12

    View Slide

  66. Saturday, August 25, 12

    View Slide

  67. OAuth
    Saturday, August 25, 12

    View Slide

  68. Saturday, August 25, 12

    View Slide

  69. ASP.Net Web API
    Saturday, August 25, 12

    View Slide

  70. Content Negotiation
    public  HttpResponseMessage  GetProduct(int  id)
    {
           var  product  =  new  Product()  
                   {  Id  =  id,  Name  =  "Gizmo",  Category  =  "Widgets",  Price  =  1.99M  };
           IContentNegotiator  negotiator  =  this.Configuration.Services.GetContentNegotiator();
           ContentNegotiationResult  result  =  negotiator.Negotiate(
                   typeof(Product),  this.Request,  this.Configuration.Formatters);
           if  (result  ==  null)
           {
                   var  response  =  new  HttpResponseMessage(HttpStatusCode.NotAcceptable);
                   throw  new  HttpResponseException(response));
           }
           return  new  HttpResponseMessage()
           {
                   Content  =  new  ObjectContent(
                           product,                                        //  What  we  are  serializing  
                           result.Formatter,                      //  The  media  formatter
                           result.MediaType.MediaType    //  The  MIME  type
                   )
           };
    }
    Saturday, August 25, 12

    View Slide

  71. Saturday, August 25, 12

    View Slide

  72. spkr8.com/neraath
    Thanks!
    Saturday, August 25, 12

    View Slide