ASP.NET Web API

21
ASP.NET Web API Udaiappa Ramachandran NHDN-Nashua .NET/Cloud Computing UG Lead Email: [email protected] Blog: http://cloudycode.wordpress.com

description

ASP.NET Web API. Udaiappa Ramachandran NHDN-Nashua .NET/Cloud Computing UG Lead Email: [email protected] 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 - PowerPoint PPT Presentation

Transcript of ASP.NET Web API

Page 1: ASP.NET Web API

ASP.NET Web API

Udaiappa RamachandranNHDN-Nashua .NET/Cloud Computing UG LeadEmail: [email protected]: http://cloudycode.wordpress.com

Page 2: ASP.NET Web API

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

Page 3: ASP.NET Web API

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

Page 4: ASP.NET Web API

Demo: Web API Basics

Page 5: ASP.NET Web API

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 HttpGet, HttpPut, HttpPost or HttpDelete attribute

Action can be defined thru AcceptVerbs as a method attributes Route by Action Name Override action name by using ActionName attribute Ignore action by NoAction Attribute

Page 6: ASP.NET Web API

Demo: Web API Routing

Page 7: ASP.NET Web API

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.AspNet.WebApi.OData -Pre

Page 8: ASP.NET Web API

Demo: Web API Filtering

Page 9: ASP.NET Web API

Web API Pipeline Both Request and Response Model have pipeline Web API has client and server side pipelines Both Share the common object HttpMessageHandler HttpRequestMessage

Represents all info about http request such as URL, HTTP Methods and Headers HttpResponseMessage

Represents all the info about the Http Response such as StatusCode, Success Flag, Original Http Request

HttpMessageHandler 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 DelegatingHandler Custom code to perform any kind of per request functionality

Page 10: ASP.NET Web API

Pipeline Elements

Page 11: ASP.NET Web API

Pipeline Elements

Page 12: ASP.NET Web API

Demo: Web API Pipeline

Demo: Custom Handlers

Page 13: ASP.NET Web API

Dependency Resolution Service Oriented Architecture for Decoupling the

object IoC: The concept of allowing something outside of

system to control it. Often done thru DI frameworks

UnityNinjectCastle Windsor Structure MapSpring.netAutofac

Web API provides a hook for us to wire up the complex dependency

Page 14: ASP.NET Web API

Demo: Simple Resolver

Demo: Resolve using DI Framework

Page 15: ASP.NET Web API

Data Model and MIME types Returning JSON

Default return formatGood for mobile appsdoes not require any special code

Returning XMLNo code changeSet the request content type as application/xml

Mime Types:return virtually anything that can be sent over the webInherit from BufferedMediaTypeFormatterOverride key methodsattach media formatter to configuration

Page 16: ASP.NET Web API

Demo: Data Model

Demo: MIME Types

Page 17: ASP.NET Web API

Hosting IIS

very common hosting solutionset up just like a web appcalls are made via URLs handled by the web

server Self Hosting

Not restricted to IIScan host in your own processuses the HttpSelfHostConfigurationUseful for tightly focused services and apps

Page 18: ASP.NET Web API
Page 19: ASP.NET Web API

Demo: Self Hosting

Page 21: ASP.NET Web API

Q & A