ASP NET Web API Udaiappa Ramachandran NHDNNashua NETCloud

ASP. NET Web API Udaiappa Ramachandran NHDN-Nashua. NET/Cloud Computing UG Lead Email: udaiappa@gmail. com Blog: http: //cloudycode. wordpress. com

Agenda � Introduction to Web API � Web API Routing � Web API Pipeline � Dependency Injections and Resolvers � Model Binding and Media Formats � Self Hosting � Deploying Web API into cloud � References

Introduction to Web API � Next iteration of WCF REST � Framework for developing REST Services � Released part of ASP. NET MVC � Uses HTTP protocol � Accessible from wide variety of clients � Http Methods: GET, POST, PUT, DELETE � URLs and Methods

Demo: Web API Basics

Web API Routing � � � Web API Routing very similar to MVC routing Action determined using the HTTP method not the URI path Uses “api” in the rout is to avoid collisons with ASP. NET MVC routing. Adds “Controller” to the controller part of the URL Default mapping thru global. asax Additional URL parameters are mapped as action parameters Explicitly specify the HTTP methods for an action by decorating the action method with Http. Get, Http. Put, Http. Post or Http. Delete attribute Action can be defined thru Accept. Verbs as a method attributes Route by Action Name Override action name by using Action. Name attribute Ignore action by No. Action Attribute

Demo: Web API Routing

Filtering � Uses OData specific keywords $expand Directs that related records should be retrieved in the record or collection being retrieved. $filter Specifies an expression or function that must evaluate to ‘true’ for a record to be returned in the collection. $orderby Determines what values are used to order a collection of records. $select Specifies a sub set of properties to return. $skip Sets the number of records to skip before it retrieves records in a collection. $top Determines the maximum number of records to return. Install-Package Microsoft. Asp. Net. Web. Api. OData -Pre

Demo: Web API Filtering

Web API Pipeline � � Both Request and Response Model have pipeline Web API has client and server side pipelines Both Share the common object Http. Message. Handler Http. Request. Message � Represents all info about http request such as URL, HTTP Methods and Headers � Http. Response. Message � Represents all the info about the Http Response such as Status. Code, Success Flag, Original Http Request � Http. Message. Handler � Common for both Request and Response � Most common processing code can be placed here � Ideal for authentication � Two Scopes: Global and Per-Route � Custom Handlers � Inherit Delegating. Handler � Custom code to perform any kind of per request functionality

Pipeline Elements

Pipeline Elements

Demo: Web API Pipeline Demo: Custom Handlers

Dependency Resolution Service Oriented Architecture for Decoupling the object � Io. C: The concept of allowing something outside of system to control it. � Often done thru DI frameworks � �Unity �Ninject �Castle Windsor � Structure Map �Spring. net �Autofac � Web API provides a hook for us to wire up the complex dependency

Demo: Simple Resolver Demo: Resolve using DI Framework

Data Model and MIME types � Returning JSON �Default return format �Good for mobile apps �does not require any special code � Returning XML �No code change �Set the request content type as application/xml � Mime Types: �return virtually anything that can be sent over the web �Inherit from Buffered. Media. Type. Formatter �Override key methods �attach media formatter to configuration

Demo: Data Model Demo: MIME Types

Hosting � IIS � � Self Hosting � �


Demo: Self Hosting

Resource � � � http: //cloudycode. wordpress. com http: //www. asp. net/web-api/overview http: //code. msdn. microsoft. com/ASPNET-Web-API-Self. Host-30 abca 12 http: //msdn. microsoft. com/en-us/library/gg 309461. aspx http: //blogs. msdn. com/b/webdev/archive/2012/08/26/aspnet-web-api-and-httpclient-samples. aspx

Q&A
- Slides: 21