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
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)
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)
CONSULTING · DEVELOPMENT · TRAINING In other words • HTTP messages not only transport data, but also carry metadata and semantics that are application independent.
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
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.
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+” } );
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.
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
CONSULTING · DEVELOPMENT · TRAINING HTTP Verb Routing • Method name starts with “Get”, “Post”, “Put”, “Delete”, “Head”, “Options” or “Patch”. • Decorate with AcceptVerbs, HttpGet, HttpDelete etc..
CONSULTING · DEVELOPMENT · TRAINING Model/Parameter Binding • Default Behavior: - Simple Types: From URI using Model Binding - Complex Types: From Body using Formatters
CONSULTING · DEVELOPMENT · TRAINING Model/Parameter Binding • The default behavior is overridable For more information: http://bit.ly/webApiParameterBinding http://bit.ly/webApiParameterBinding2
CONSULTING · DEVELOPMENT · TRAINING Content-Negotiation • The client can request that the response should use a specific Media Type by using the Accept header.
CONSULTING · DEVELOPMENT · TRAINING HttpMessageHandler HttpMessageHandler HttpMessageHandler HTTP Server HttpRoutingDispatcher : HttpMessageHandler HttpMessageHandler (Route specific) HttpControllerDispatcher : HttpMessageHandler Controller Media Type Formatter Conneg
CONSULTING · DEVELOPMENT · TRAINING HttpMessageHandler • Extension point to deal with HTTP Message level extensions E.g. Check for Auth Token, HTTP Caching
CONSULTING · DEVELOPMENT · TRAINING Media Type Formatter • Responsible for Serialization/Deserialization of Resource/.Net Type into Media Type; • Web API ships with JSON and XML;
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.