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

REST Easy - Building RESTful Web Services using...

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. Me • Fightin’ Texas Aggie • C# Developer since 2005

    • SharePoint and Identity Management Architect • Senior Consultant at Improving Enterprises • [email protected] Saturday, August 25, 12
  2. Twitter Promotion • @neraath • #houcodecamp • #rest • #thisguyisawesome

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

    • #bestspeakerever • #horriblepresenter • #omgshootme Saturday, August 25, 12
  4. Agile, Microsoft, Open Technologies, UX Applied Training, Coaching, Mentoring Certified

    Consulting Rural Sourcing Recruiting Services Saturday, August 25, 12
  5. SOAP is Fugly <?xml version="1.0"?> <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"> <soap:Header> </soap:Header> <soap:Body>

    <m:GetStockPrice xmlns:m="http://www.example.org/stock"> <m:StockName>IBM</m:StockName> </m:GetStockPrice> </soap:Body> </soap:Envelope> Saturday, August 25, 12
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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 <user xmlns:atom="http://www.w3.org/2005/Atom"> <id>user001</id> <atom:link rel="self" href="http://example.org/user/user001"/> <name>John Doe</name> <email>[email protected]</email> </user> Saturday, August 25, 12
  12. Multiple Language Support # Response HTTP/1.1 200 OK Content-Language: kr

    <address type="work"> <street-address>강남구 삼성동 144-19,20 번지 JS 타워</street-address> <locality>서울특별시</locality> <postal-code>135-090</postal-code> <country-name>대한민국</country-name> <country-code>KR</country-code> </address> Saturday, August 25, 12
  13. # Request GET /user/001/followers HTTP/1.1 Accept: application/json,*/*;q=0.0 # Response 406

    Not Acceptable Content-Type: application/json Link: <http://www.example.org/errors/mediatypes.html>;rel="help" { "message" : "This server does not support JSON." } Saturday, August 25, 12
  14. 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 <error xmlns:atom=”http://www.w3.org/2005/Atom”> <message>Unauthorized.</message> </error> Saturday, August 25, 12
  15. 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
  16. 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 <error xmlns:atom=”http://www.w3.org/2005/Atom”> <message>Unauthorized.</message> </error> Saturday, August 25, 12
  17. 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
  18. 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
  19. 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>(                        product,                                        //  What  we  are  serializing                          result.Formatter,                      //  The  media  formatter                        result.MediaType.MediaType    //  The  MIME  type                )        }; } Saturday, August 25, 12