building services for any client with asp net web api n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Building services for any client with ASP.NET Web API PowerPoint Presentation
Download Presentation
Building services for any client with ASP.NET Web API

Loading in 2 Seconds...

play fullscreen
1 / 40

Building services for any client with ASP.NET Web API - PowerPoint PPT Presentation


  • 97 Views
  • Uploaded on

Building services for any client with ASP.NET Web API. Daniel Roth Senior Program Manager 3-036. How to reach more clients?. App. Devices. Browsers. Phones. ?. ?. ?. ?. Tablets. How to make it scale?. App. Devices. Browsers. Phones. ?. ?. ?. ?. Tablets.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Building services for any client with ASP.NET Web API' - otto-wilder


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
building services for any client with asp net web api

Building services for any client with ASP.NET Web API

Daniel Roth

Senior Program Manager

3-036

how to reach more clients
How to reach more clients?

App

Devices

Browsers

Phones

?

?

?

?

Tablets

how to make it scale
How to make it scale?

App

Devices

Browsers

Phones

?

?

?

?

Tablets

leverage the web build web apis
Leverage the web – build Web APIs

App

Web API

Devices

Browsers

Phones

Tablets

what is a web api
What is a Web API

An HTTP service

Designed for broad reach

Uses HTTP as an application protocol, not a transport protocol

asp net web api

ASP.NET Web API

  • A framework for creating HTTP services that can reach a broad range of clients including browsers and mobile devices
slide7
Microsoft Office 2013Unified communications Web API Powering Lync Web App 2013 and Lync 2013 for Windows Phone 8
unified communications web api ucwa
Unified Communications Web API (UCWA)

UI

A

P

P

U

I

Model

Media Manager

(Media State)

M

O

D

E

L

SRTP

A

P

I

REST API

Lync Server 2013

UCWA

ASP.NET WEB API

WE

B

RO

L

E

asp net web api1

ASP.NET Web API

  • First-class modern HTTP
  • programming model
  • Easily map resources to URIs and implement the uniform interface of HTTP
  • Rich support for formats and HTTP content negotiation
  • Enable hypermedia and link generation
  • Separate out cross cutting concerns
  • Flexible hosting
  • Light-weight, testable, scales
getting started with asp net web api
Getting started with ASP.NET Web API
  • Included with ASP.NET MVC 4 and Visual Studio 2012
  • Available as stand-alone NuGet packages
  • Supported on .NET 4 and later
  • Documentation on http://www.asp.net/web-api
  • Install the ASP.NET Fall 2012 Update preview (http://www.asp.net/vnext) to get the latest features
creating web apis
Creating Web APIs
  • Define your routes (or just use the default one)
  • Create a new class that derives from APIController
  • Name your class {controller} + Controller
  • Implement your actions

routes.MapHttpRoute(

name: "DefaultApi",

routeTemplate: "api/{controller}/{id}",

defaults: new { id = RouteParameter.Optional});

web api dispatcher
Web API dispatcher

Invoke action

Action filters

Parameter binding

Authorization filters

Select action

Exception filters

Route to controller

Formatting

HttpRequestMessage

HttpResponseMessage

web api dispatcher1
Web API dispatcher

Invoke action

Action filters

Parameter binding

Authorization filters

Select action

Exception filters

Route to controller

Formatting

HttpRequestMessage

HttpResponseMessage

implementing web api actions
Implementing Web API actions
  • Web API actions map to HTTP methods
  • Prefix your action name with the desired HTTP method
    • Ex GetCustomer, PostComment, DeleteOrder, etc.
  • Use [HttpGet/Post/Put/Delete] if you prefer a different name
  • Overload actions based on the presence of data in the URI
  • Note: Action selection can also be done using the {action} route variable just like MVC

IEnumerable<Customer> Get() {…}

Customer Get(int id) {…}

web api dispatcher2
Web API dispatcher

Invoke action

Action filters

Parameter binding

Authorization filters

Select action

Exception filters

Route to controller

Formatting

HttpRequestMessage

HttpResponseMessage

web api dispatcher3
Web API dispatcher

Invoke action

Action filters

Parameter binding

Authorization filters

Select action

Exception filters

Route to controller

Formatting

HttpRequestMessage

HttpResponseMessage

action parameters
Action parameters
  • Simple parameter types are taken from the URI
    • Route data, query parameters
  • Complex parameter types come from the body (only one)
    • Configured MediaTypeFormatters (formatters) are used to deserializethe request body based on the content type
    • JSON, XML, and form–url–encoded supported by default
  • Override using [FromUrl], [FromBody], [ModelBinder], custom parameter binding
  • You can always work with HttpRequestMessage directly
parameter validation
Parameter validation
  • Validation is run on the bound parameters
  • Specify validation rules using DataAnnotations or custom validation logic
  • Validation errors are accumulated in the ModelState
  • Check ModelState.IsValid
  • Send back validation errors to client using Request.CreateErrorResponse(ModelState)
web api dispatcher4
Web API dispatcher

Invoke action

Action filters

Parameter binding

Authorization filters

Select action

Exception filters

Route to controller

Formatting

HttpRequestMessage

HttpResponseMessage

web api dispatcher5
Web API dispatcher

Invoke action

Action filters

Parameter binding

Authorization filters

Select action

Exception filters

Route to controller

Formatting

HttpRequestMessage

HttpResponseMessage

web api dispatcher6
Web API dispatcher

Invoke action

Action filters

Parameter binding

Authorization filters

Select action

Exception filters

Route to controller

Formatting

HttpRequestMessage

HttpResponseMessage

response formatting and content negotiation
Response formatting and content negotiation
  • Returned object is serialized in the response body using configured formatters
  • Request indicates preferred format using the accept header
  • Server selects a format for the response based on the request, action return type, configured formatters
  • You can always just return HttpResponseMessage directly
web api dispatcher7
Web API dispatcher

Invoke action

Action filters

Parameter binding

Authorization filters

Select action

Exception filters

Route to controller

Formatting

HttpRequestMessage

HttpResponseMessage

filters
Filters
  • Used to handle cross cutting concerns
    • Action filters run before and after invoking an action
    • Authorization filters run before model binding and are specifically for authorizing the user
    • Exception filters handle generating responses for error cases in a centralized way
  • Configured globally, per controller, or per action as an attribute (ex [Authorize])
asp net web api odata
ASP.NET Web API OData
  • Query
    • Simply return IQueryable to enable OData query support
    • Add an ODataQueryOptions parameter to manage the query yourself
  • Paging
    • Use [Queryable(ResultLimit=10)] to limit the number of results
    • Add the new ODataMediaTypeFormatter to enable next links using the OData format
asp net web api odata1
ASP.NET Web API OData

Coming post Dec

Select, expand

Containment

Functions

Change sets

Named streams

Open types

Etags

Recursive routing

  • Features
    • CRUD operations
    • Query
    • Atom and JSON formats
    • Actions
    • Model builder
    • Service Document, $metadata
    • Inheritance
    • Collection properties
when to use asp net web api odata
When to use ASP.NET Web API OData
  • Non-SQL data sources
  • Support as much query as you want
  • Lots of custom business logic
http request response handling
HTTP request/response handling

Message handlers

Message handlers

Host

Http Server

Http

Dispatcher

HttpClient

Handler

Http Client

  • Message handlers:
  • Task<HttpResponseMessage> SendAsync(
    • HttpRequestMessagerequest,
    • CancellationTokencancellationToken)
supporting multiple clients
Supporting multiple clients

Web API

Single Page App

supporting multiple clients1
Supporting multiple clients

Web API

Windows Phone App

Windows Store App

Single Page App

summary
Summary
  • ASP.NET Web API is a framework for building HTTP services that can reach any client
    • It’s great for building Azure hosted back ends for Windows Store and Windows Phone apps
  • Try out the new Single Page Application, OData, Help Page, and Tracing support in the ASP.NET Fall 2012 Update preview
resources

Resources

  • http://www.asp.net/web-api
  • http://aspnetwebstack.codeplex.com

Please submit session evals on the Build Windows 8 App or at http://aka.ms/BuildSessions

resources1

Resources

  • Follow us on Twitter @WindowsAzure
  • Get Started: www.windowsazure.com/build

Please submit session evals on the Build Windows 8 App or at http://aka.ms/BuildSessions