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

Introduction to ASP.NET Web API

Sponsored · SiteGround - Reliable hosting with speed, security, and support you can count on. →
Avatar for Pedro Reys Pedro Reys
November 20, 2012

Introduction to ASP.NET Web API

This was a talk given by Pedro Reys and Chris Missal as part of a public Headspring Brownbag

Avatar for Pedro Reys

Pedro Reys

November 20, 2012

Other Decks in Programming

Transcript

  1. Introduction to ASP.NET Web API Chris Missal & Pedro Reys

    CONSULTING · DEVELOPMENT · TRAINING ASP.NET Web API Headspring Brown Bag
  2. Introduction to ASP.NET Web API Chris Missal & Pedro Reys

    CONSULTING · DEVELOPMENT · TRAINING headspring.com/chris headspring.com/pedro
  3. Introduction to ASP.NET Web API Chris Missal & Pedro Reys

    CONSULTING · DEVELOPMENT · TRAINING MVC Boot Camp http://bit.ly/mvcbootcamp
  4. Introduction to ASP.NET Web API Chris Missal & Pedro Reys

    CONSULTING · DEVELOPMENT · TRAINING Web API? • Also Known As: - REST API - RESTFul API - Hypermedia API - HTTP API • My own definition of Web API (Not REST) - API that embraces the HTTP protocol
  5. Introduction to ASP.NET Web API Chris Missal & Pedro Reys

    CONSULTING · DEVELOPMENT · TRAINING Let’s start with HTTP
  6. Introduction to ASP.NET Web API Chris Missal & Pedro Reys

    CONSULTING · DEVELOPMENT · TRAINING HTTP is not RPC “What makes HTTP significantly different from RPC is that the requests are directed to resources using a generic interface with standard semantics that can be interpreted by intermediaries almost as well as by the machines that originate services.” FIELDING (6.5.2)
  7. Introduction to ASP.NET Web API Chris Missal & Pedro Reys

    CONSULTING · DEVELOPMENT · TRAINING HTTP is not a Transport Protocol “HTTP is not designed to be a transport protocol. (…) A true application of HTTP maps the protocol user's actions to something that can be expressed using HTTP semantics, thus creating a network-based API to services which can be understood by agents and intermediaries without any knowledge of the application.” FIELDING (6.5.3)
  8. Introduction to ASP.NET Web API Chris Missal & Pedro Reys

    CONSULTING · DEVELOPMENT · TRAINING In other words • HTTP messages not only transport data, but also carry metadata and semantics that are application independent.
  9. Introduction to ASP.NET Web API Chris Missal & Pedro Reys

    CONSULTING · DEVELOPMENT · TRAINING In case you want to learn more about this: • Roy Fielding’s PhD Dissertation http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm
  10. Introduction to ASP.NET Web API Chris Missal & Pedro Reys

    CONSULTING · DEVELOPMENT · TRAINING • If y’all want to discuss REST and Hypermedia APIs we can schedule another Brown Bag to do it.
  11. Introduction to ASP.NET Web API Chris Missal & Pedro Reys

    CONSULTING · DEVELOPMENT · TRAINING But we were talking about ASP.NET Web API
  12. Introduction to ASP.NET Web API Chris Missal & Pedro Reys

    CONSULTING · DEVELOPMENT · TRAINING • Started as WCF Web API to make the REST story better on WCF wcf.codeplex.com • Moved to ASP.NET and is now part of the ASP.NET Web Stack aspnetwebstack.codeplex.com • Available with MVC installer and as Nuget packages.
  13. Introduction to ASP.NET Web API Chris Missal & Pedro Reys

    CONSULTING · DEVELOPMENT · TRAINING ASP.NET Web API key features • HTTP Verb based routing • Content-Negotiation • Self and IIS hosting
  14. Introduction to ASP.NET Web API Chris Missal & Pedro Reys

    CONSULTING · DEVELOPMENT · TRAINING Routing: Route Templates api/{controller}/{id}
  15. Introduction to ASP.NET Web API Chris Missal & Pedro Reys

    CONSULTING · DEVELOPMENT · TRAINING Routing: Defaults and Constraints Routes.MapHttpRoute( name: “MyRoute”, routeTemplate: “api/{controller}/{category}/{id}, defaults: new { category = all, id = RouteParameter.Optional }, constraints: new{ id = @”\d+” } );
  16. Introduction to ASP.NET Web API Chris Missal & Pedro Reys

    CONSULTING · DEVELOPMENT · TRAINING Routing: Controller Selection • Same convention as ASP.NET MVC: - It will pull the “controller” value out of the route dictionary, append “Controller” to it and look for Web API Controllers that matches that name.
  17. Introduction to ASP.NET Web API Chris Missal & Pedro Reys

    CONSULTING · DEVELOPMENT · TRAINING Routing: Action Selection
  18. Introduction to ASP.NET Web API Chris Missal & Pedro Reys

    CONSULTING · DEVELOPMENT · TRAINING {action}? Name matches? Y 404 N HTTP Verb Supported? Y Find Action matching HTTP Verb N 405 N Find Actions that matches Route and Request Parameters? Y 404 N Single Action? Y Action Found! Y N Error
  19. Introduction to ASP.NET Web API Chris Missal & Pedro Reys

    CONSULTING · DEVELOPMENT · TRAINING HTTP Verb Routing • Method name starts with “Get”, “Post”, “Put”, “Delete”, “Head”, “Options” or “Patch”. • Decorate with AcceptVerbs, HttpGet, HttpDelete etc..
  20. Introduction to ASP.NET Web API Chris Missal & Pedro Reys

    CONSULTING · DEVELOPMENT · TRAINING Model/Parameter Binding • Default Behavior: - Simple Types: From URI using Model Binding - Complex Types: From Body using Formatters
  21. Introduction to ASP.NET Web API Chris Missal & Pedro Reys

    CONSULTING · DEVELOPMENT · TRAINING Model/Parameter Binding • The default behavior is overridable For more information: http://bit.ly/webApiParameterBinding http://bit.ly/webApiParameterBinding2
  22. Introduction to ASP.NET Web API Chris Missal & Pedro Reys

    CONSULTING · DEVELOPMENT · TRAINING Content-Negotiation • The client can request that the response should use a specific Media Type by using the Accept header.
  23. Introduction to ASP.NET Web API Chris Missal & Pedro Reys

    CONSULTING · DEVELOPMENT · TRAINING GET /people Accept: application/json HTTP/1.1 200 OK Content-Type: application/json [ {“title”: “Weekly Meeting”, “id” : 1} ]
  24. Introduction to ASP.NET Web API Chris Missal & Pedro Reys

    CONSULTING · DEVELOPMENT · TRAINING GET /people Accept: text/xml HTTP/1.1 200 OK Content-Type: text/html <meetings> <meeting title=“Weekly Meeting” id=“1” /> </meetings>
  25. Introduction to ASP.NET Web API Chris Missal & Pedro Reys

    CONSULTING · DEVELOPMENT · TRAINING You can create your own Media Type: GET /meetings Accept: application/vnd.headspring.com
  26. Introduction to ASP.NET Web API Chris Missal & Pedro Reys

    CONSULTING · DEVELOPMENT · TRAINING HTTP Request Pipeline
  27. Introduction to ASP.NET Web API Chris Missal & Pedro Reys

    CONSULTING · DEVELOPMENT · TRAINING HttpMessageHandler HttpMessageHandler HttpMessageHandler HTTP Server HttpRoutingDispatcher : HttpMessageHandler HttpMessageHandler (Route specific) HttpControllerDispatcher : HttpMessageHandler Controller Media Type Formatter Conneg
  28. Introduction to ASP.NET Web API Chris Missal & Pedro Reys

    CONSULTING · DEVELOPMENT · TRAINING HttpMessageHandler • Extension point to deal with HTTP Message level extensions E.g. Check for Auth Token, HTTP Caching
  29. Introduction to ASP.NET Web API Chris Missal & Pedro Reys

    CONSULTING · DEVELOPMENT · TRAINING Media Type Formatter • Responsible for Serialization/Deserialization of Resource/.Net Type into Media Type; • Web API ships with JSON and XML;
  30. Introduction to ASP.NET Web API Chris Missal & Pedro Reys

    CONSULTING · DEVELOPMENT · TRAINING ASP.NET MVC extensions are still available • If you have some extensions that need to be done at the Controller or Action level, use Action Filters for that.
  31. Introduction to ASP.NET Web API Chris Missal & Pedro Reys

    CONSULTING · DEVELOPMENT · TRAINING Demo
  32. Introduction to ASP.NET Web API Chris Missal & Pedro Reys

    CONSULTING · DEVELOPMENT · TRAINING MVC Boot Camp http://bit.ly/mvcbootcamp
  33. Introduction to ASP.NET Web API Chris Missal & Pedro Reys

    CONSULTING · DEVELOPMENT · TRAINING THANK YOU!