Presentation is loading. Please wait.

Presentation is loading. Please wait.

Building HTTP Services with ASP.NET Web API

Similar presentations


Presentation on theme: "Building HTTP Services with ASP.NET Web API"— Presentation transcript:

1 Building HTTP Services with ASP.NET Web API
1/16/ :16 AM DEV309 Building HTTP Services with ASP.NET Web API Daniel Roth Senior Program Manager Microsoft Corporation © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

2 What is a Web API? An HTTP service Designed for broad reach
1/16/ :16 AM What is a Web API? An HTTP service Designed for broad reach Uses HTTP as an application protocol, not a transport protocol ©2005 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.

3 Why build Web APIs?

4 Reach More Clients

5 Scale with the Cloud

6 Embrace HTTP GET /en/html/dummy.php?name=MyName&married=not+single &male=yes HTTP/1.1 Host: User-Agent: Mozilla/5.0 (Windows;en-GB; rv: ) Gecko/ Firefox/ Accept: text/xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5 Accept-Language: en-gb,en;q=0.5 Accept-Encoding: gzip,deflate Accept-Charset: ISO ,utf-8;q=0.7,*;q=0.7 Keep-Alive: 300 Connection: keep-alive Referer:

7 Contact Manager browser and mobile app
1/16/ :16 AM demo Contact Manager browser and mobile app Daniel Roth Senior Program Manager Microsoft Corporation © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

8 Web API framework requirements
Need a first class HTTP programming model Easily map resources to URIs and implement the uniform interface Rich support for formats and HTTP content negotiation Separate out cross cutting concerns Light weight, testable, scales

9 So you want to create a Web API . . .
? ASP.NET MVC WCF Web HTTP WCF REST Starter Kit WCF Web API

10 So you want to create a Web API . . .
+ ASP.NET MVC WCF Web HTTP WCF REST Starter Kit WCF Web API

11 So you want to create a Web API . . .
ASP.NET Web API

12 ASP.NET Web API Features
From ASP.NET MVC From WCF Web API ASP.NET Routing Model binding Validation Filters Link generation Testability IoC integration VS template Scaffolding Modern HTTP programming model HttpClient Task-based async Formatting, content negotiation Server-side query composition Create custom help pages Self-host Tracing

13

14 We are open source! http://aspnetwebstack.codeplex.com

15 Daniel Roth Senior Program Manager Microsoft Corporation
demo Your First Web API Daniel Roth Senior Program Manager Microsoft Corporation © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

16 To implement a Web API . . . Derive from ApiController
Implement your actions Actions map to HTTP methods Prefix method name with desired HTTP method – PostComment Use [HttpGet/Post/Put/Delete] if you prefer a different name

17 Routing Maps a URI space to your ApiControllers
Ex. api/{controller}/{id} {controller} + “Controller” = ApiController type name Can be tailored using default values and route constraints

18 Default Web API route name: "DefaultApi",
routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } );

19 Action parameters Simple types are taken from the URI
Route data, query parameters Complex types come from the body Configured MediaTypeFormatters are used to deserialize the request body based on the content type JSON, XML, and form-url-encoded supported out of the box Override using [FromUrl], [FromBody], [ModelBinder], custom parameter binding

20 Validation Validation is run on the data from every request
Validation errors are accumulated in the ModelState Check ModelState.IsValue Uses DataAnnotations or custom validation logic

21 Content Negotiation Response format is determined based on HTTP content negotiation Accept header in request expresses desired format(s) Server selects a format for the response based on the: Request Action return type Configured MediaTypeFormatters JSON and XML supported out of the box

22 Content Negotiation // Get the IContentNegotiator
IContentNegotiator negotiator = Configuration.Services.GetContentNegotiator(); // Run content negotiation to select a formatter MediaTypeHeaderValue mediaType; MediaTypeFormatter formatter = negotiator.Negotiate( typeof(Contact), Request, Configuration.Formatters, out mediaType); // Create a response message with an object content using the selected formatter HttpResponseMessage response = new HttpResponseMessage() { Content = new ObjectContent<Contact>(contact, formatter), RequestMessage = Request };

23 Content Negotiation HttpResponseMessage response =
Request.CreateResponse<Contact>(HttpStatusCode.Created, contact);

24 Scaffolding an EF-based web API
1/16/ :16 AM demo Scaffolding an EF-based web API Daniel Roth Senior Program Manager Microsoft Corporation © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

25 Filters Filters are used to handle cross cutting concerns
Filter types: 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 Filters can be configured globally, per controller, or per action as an attribute

26 Authorization filters
HTTP Dispatcher Invoke Action Action filters Model Bind Authorization filters Select action Exception filters Route to controller Formatting Request Response

27 OData queries Support for OData URL query syntax
$top, $skip, $orderby, $filter Enable query using [Queryable] filter and returning IQueryable<T> from a method Orthogonal to format and content negotiation

28 Daniel Roth Senior Program Manager Microsoft Corporation
demo OData query support Daniel Roth Senior Program Manager Microsoft Corporation © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

29 HTTP request/response handling
Message handlers Message handlers Http Client HttpClient Handler Host Http Server Http Dispatcher Message handlers: Task<HttpResponseMessage> SendAsync( HttpRequestMessage request, CancellationToken cancellationToken)

30 Daniel Roth Senior Program Manager Microsoft Corporation
demo URI format extensions Daniel Roth Senior Program Manager Microsoft Corporation © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

31 Web API description Use the IApiExplorer service to get a runtime description of the Web API Useful for building custom help pages, test clients, and tooling

32 IApiExplorer IApiExplorer apiExplorer =
config.Services.GetApiExplorer(); public interface IApiExplorer { Collection<ApiDescription> ApiDescriptions { get; } }

33 Custom web API help page
1/16/ :16 AM demo Custom web API help page Daniel Roth Senior Program Manager Microsoft Corporation © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

34 Hosting Two hosting options today
ASP.NET Web Application (IIS) Self-host server option (ex. console application, Windows service, Azure Worker Role, etc.) HttpConfiguration is the common denominator Host in memory for end-to-end testing Use an OWIN bridge to host on any OWIN compatible web server

35 Daniel Roth Senior Program Manager Microsoft Corporation
demo Flexible hosting Daniel Roth Senior Program Manager Microsoft Corporation © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

36 The Road Ahead ASP.NET Web API ships with ASP.NET MVC 4
Supported on .NET 4 Ships in the box with Visual Studio 2012 Release Candidate available via Web Platform Installer, NuGet, Visual Studio 2012 RC Targeting RTM in Q3 of 2012

37 Summary ASP.NET Web API in MVC 4 makes it easy to build HTTP services that can reach a broad set of clients ASP.NET Web API is an ideal platform for creating RESTful services Try out the ASP.NET MVC 5 Release Candidate Follow our progress on CodePlex

38 Related Content DEV301 ASP.NET Roadmap: One ASP.NET – Web Forms, MVC, Web API, and more DEV302 What's New in Visual Studio 2012 for Web Developers DEV303 ASP.NET Loves HTML5 DEV304 ASP.NET for Mobile and Tablet Development DEV305 Microsoft ASP.NET and the Realtime Web

39 DEV Track Resources Visual Studio Home Page :: Somasegar’s Blog :: Jason Zander’s Blog :: Facebook :: Twitter ::

40 Resources Learning TechNet http://northamerica.msteched.com
Connect. Share. Discuss. Microsoft Certification & Training Resources TechNet Resources for IT Professionals Resources for Developers

41 Complete an evaluation on CommNet and enter to win!
Required Slide Complete an evaluation on CommNet and enter to win!

42 Please Complete an Evaluation Your feedback is important!
Be eligible to win great daily prizes and the grand prize of a $5,000 Travel Voucher! Multiple ways to Evaluate Sessions Scan the Tag to evaluate this session now on myTechEd Mobile

43 1/16/ :16 AM © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION. © 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

44 1/16/ :16 AM © 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.


Download ppt "Building HTTP Services with ASP.NET Web API"

Similar presentations


Ads by Google