Building HTTP Services with ASP.NET Web API

Slides:



Advertisements
Similar presentations
Building Web APIs in Windows Azure Name Title Microsoft Corporation.
Advertisements

06 | Implementing Web APIs Jon Galloway | Tech Evangelist Christopher Harrison | Head Geek.
Building HTTP Services with ASP.NET Web API Sayed Ibrahim Hashimi Program Manager Microsoft Corporation DEV309.
06 | HTTP Services with Web API Bruno Terkaly | Technical Evangelist Bret Stateham | Technical Evangelist.
demo Demo.
What’s New with IIS 8: Open Web Platform for Cloud
5/15/2018 © 2014 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks.
Data centric apps for web, desktop and mobile with EF5
TechEd /1/2018 7:56 AM © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks.
The Modern ASP.NET Tech Stack!
Jim Nakashima Program Manager – Cloud Tools Microsoft Corporation
6/26/2018 9:02 PM © 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered.
What web developers need to know when building Metro style apps
Building real-time web apps with WebSockets using IIS, ASP.NET and WCF
Microsoft Build /11/2018 2:12 AM © 2016 Microsoft Corporation. All rights reserved. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY,
Building Modern Web Apps with ASP.NET MVC 6
Enriching your BI Semantic Models with Data Analysis Expressions (DAX)
Building the Perfect BI Semantic Model for Power View
HTML5 Platform from Web to Apps
11/21/2018 4:57 AM SIA303 Advanced Persistent Threats (APT): Understanding the New Era of Attacks! Marcus Murray Security Team Manager, Microsoft MVP –
ASP.NET Roadmap Mike Ormond Developer & Platform Group Microsoft Ltd
What’s new in Visual Studio for web developers
Jason Zander Unplugged
Visual Studio 2013 for web developers
12/2/2018 4:10 AM © 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered.
Using Windows Runtime and SDK to build Metro style apps
TechEd /6/2018 8:16 AM © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks.
Deep Dive into the Team Foundation Server 2012 Agile Planning Tools
Team Foundation Server 2010 for Everyone
Your Devices + OData + Azure = 
TechEd /9/2018 4:17 AM © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks.
MIX 09 12/9/2018 6:08 PM © 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered.
Data Driven ASP.NET Web Forms Applications Deep Dive
Advanced IntelliTrace in Production
TechEd /11/ :21 PM © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered.
DEV410: Deep Dive into Team Foundation Server 2012 Reporting
TechEd /15/2019 8:08 PM © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks.
Building HTTP services for modern client apps
TechEd /18/ :08 AM © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered.
Building Awesome Metro style HTML apps in Blend
2/17/2019 7:32 AM © 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered.
Integrating with Windows 8 Experiences
TechEd /22/2019 9:22 PM © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks.
Building Silverlight Apps with RIA Services
From Development to Production: Optimizing for Continuous Delivery
Building Business Applications in LightSwitch
Using Windows Runtime and SDK to build Metro style apps
Create rich, data-driven Web apps with ASP.NET 4.5 Web Forms
ASP.NET Roadmap: One ASP.NET – Web Forms, MVC, Web API, and more
Creating and Consuming OData Services for Business Applications
Sayed Ibrahim Hashimi Program Manager Microsoft Corporation
Tech Ed North America /12/2019 6:45 AM Required Slide
From Development to Production: Optimizing for Continuous Delivery
Brandon Bray Principal Group Program Manager Microsoft Corporation
What’s New in Visual Studio LightSwitch
4/19/2019 1:57 AM © 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered.
Виктор Хаджийски Катедра “Металургия на желязото и металолеене”
Service Template Creation from the Ground Up
TechEd /27/2019 5:37 AM © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks.
Jason Zander Unplugged
5/1/2019 3:29 AM © 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered.
Building RESTful services using OData
Шитманов Дархан Қаражанұлы Тарих пәнінің
Code First Development in Microsoft ADO.NET Entity Framework 4.1
ASP.NET Roadmap: One ASP.NET – Web Forms, MVC, Web API, and more
7/5/2019 © 2014 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks.
7/19/2019 2:53 AM © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered.
Erik Porter Program Manager ASP.NET Microsoft Corporation
TechEd /5/2019 2:08 AM © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks.
What’s New in Visual Studio 2012 for Web Developers
Presentation transcript:

Building HTTP Services with ASP.NET Web API 1/16/2019 12: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.

What is a Web API? An HTTP service Designed for broad reach 1/16/2019 12: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.

Why build Web APIs?

Reach More Clients

Scale with the Cloud

Embrace HTTP GET /en/html/dummy.php?name=MyName&married=not+single &male=yes HTTP/1.1 Host: www.explainth.at User-Agent: Mozilla/5.0 (Windows;en-GB; rv:1.8.0.11) Gecko/20070312 Firefox/1.5.0.11 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-8859-1,utf-8;q=0.7,*;q=0.7 Keep-Alive: 300 Connection: keep-alive Referer: http://www.explainth.at/en/misc/httpreq.shtml

Contact Manager browser and mobile app 1/16/2019 12: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.

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

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

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

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

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

http://www.asp.net/web-api

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

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.

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

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

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

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

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

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

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 };

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

Scaffolding an EF-based web API 1/16/2019 12: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.

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

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

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

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.

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)

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.

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

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

Custom web API help page 1/16/2019 12: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.

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

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.

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

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

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

DEV Track Resources Visual Studio Home Page :: http://www.microsoft.com/visualstudio/en-us Somasegar’s Blog :: http://blogs.msdn.com/b/somasegar/ Jason Zander’s Blog :: http://blogs.msdn.com/b/jasonz/ Facebook :: http://www.facebook.com/visualstudio Twitter :: http://twitter.com/#!/visualstudio

Resources Learning TechNet http://northamerica.msteched.com Connect. Share. Discuss. http://northamerica.msteched.com Microsoft Certification & Training Resources www.microsoft.com/learning TechNet Resources for IT Professionals http://microsoft.com/technet Resources for Developers http://microsoft.com/msdn

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

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

1/16/2019 12: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.

1/16/2019 12: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.