1 / 40

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?. App. Devices. Browsers. Phones. ?. ?. ?. ?. Tablets. How to make it scale?. App. Devices. Browsers. Phones. ?. ?. ?. ?. Tablets.

otto-wilder
Download Presentation

Building services for any client with ASP.NET Web API

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Building services for any client with ASP.NET Web API Daniel Roth Senior Program Manager 3-036

  2. How to reach more clients? App Devices Browsers Phones ? ? ? ? Tablets

  3. How to make it scale? App Devices Browsers Phones ? ? ? ? Tablets

  4. Leverage the web – build Web APIs App Web API Devices Browsers Phones Tablets

  5. What is a Web API An HTTP service Designed for broad reach Uses HTTP as an application protocol, not a transport protocol

  6. ASP.NET Web API • A framework for creating HTTP services that can reach a broad range of clients including browsers and mobile devices

  7. Microsoft Office 2013Unified communications Web API Powering Lync Web App 2013 and Lync 2013 for Windows Phone 8

  8. 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

  9. 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

  10. 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

  11. Your first Web API

  12. 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});

  13. Web API dispatcher Invoke action Action filters Parameter binding Authorization filters Select action Exception filters Route to controller Formatting HttpRequestMessage HttpResponseMessage

  14. Web API dispatcher Invoke action Action filters Parameter binding Authorization filters Select action Exception filters Route to controller Formatting HttpRequestMessage HttpResponseMessage

  15. 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) {…}

  16. Web API dispatcher Invoke action Action filters Parameter binding Authorization filters Select action Exception filters Route to controller Formatting HttpRequestMessage HttpResponseMessage

  17. Web API dispatcher Invoke action Action filters Parameter binding Authorization filters Select action Exception filters Route to controller Formatting HttpRequestMessage HttpResponseMessage

  18. 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

  19. 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)

  20. Web API dispatcher Invoke action Action filters Parameter binding Authorization filters Select action Exception filters Route to controller Formatting HttpRequestMessage HttpResponseMessage

  21. Web API dispatcher Invoke action Action filters Parameter binding Authorization filters Select action Exception filters Route to controller Formatting HttpRequestMessage HttpResponseMessage

  22. Web API dispatcher Invoke action Action filters Parameter binding Authorization filters Select action Exception filters Route to controller Formatting HttpRequestMessage HttpResponseMessage

  23. 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

  24. Scaffolding an entity framework code-first based Web API

  25. Web API dispatcher Invoke action Action filters Parameter binding Authorization filters Select action Exception filters Route to controller Formatting HttpRequestMessage HttpResponseMessage

  26. 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])

  27. 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

  28. OData query and paging

  29. 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

  30. When to use ASP.NET Web API OData • Non-SQL data sources • Support as much query as you want • Lots of custom business logic

  31. HTTP request/response handling Message handlers Message handlers Host Http Server Http Dispatcher HttpClient Handler Http Client • Message handlers: • Task<HttpResponseMessage> SendAsync( • HttpRequestMessagerequest, • CancellationTokencancellationToken)

  32. Flexible hosting

  33. Supporting multiple clients Web API Single Page App

  34. Single Page Application (SPA) with ASP.NET Web API

  35. Supporting multiple clients Web API Windows Phone App Windows Store App Single Page App

  36. Windows Store and Windows Phone clients

  37. 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

  38. 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

  39. 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

More Related