the full power of asp.net web api

58
Eyal Vard CEO E4D Solutions L Microsoft MVP Visu C# Site: www.e4d.co. The Full Power of ASP.NET Web API

Upload: eyal-vardi

Post on 10-May-2015

57.714 views

Category:

Technology


3 download

DESCRIPTION

The Full Power of ASP.NET Web API

TRANSCRIPT

Page 1: The Full Power of ASP.NET Web API

Eyal VardiCEO E4D Solutions LTDMicrosoft MVP Visual

C#Site: www.e4d.co.il

The Full Power of ASP.NET Web API

Page 2: The Full Power of ASP.NET Web API

The Challenge

Page 3: The Full Power of ASP.NET Web API

HTML 5 )Changes The Rules of The

Game(

Page 4: The Full Power of ASP.NET Web API

Traditional apps

Browser Request

Index.html

MVC4

Traditional Request / Response for ALL rendered content and assets

Page 5: The Full Power of ASP.NET Web API

Web Application (SPA)

Initial Request

Application.htm

MVC4

RequireJS Loader

Page1 Partial.htm

IndexViewModel.js

Application.js (bootstrap)

ViewModel (#index)

ViewModel (#login)

Model (LoginModel)JSON Requests

HTML5 localstorage

Handling disconnection

Page 6: The Full Power of ASP.NET Web API

Web API Growth

Source: www.programmableweb.com – current APIs: 4,535

Page 7: The Full Power of ASP.NET Web API

Single Page Web Apps (SPA)

Page 8: The Full Power of ASP.NET Web API

WCF vs. ASP.NET Web API

SOAP WSDL HTTP

Page 9: The Full Power of ASP.NET Web API

POST SimpleService.asmx/EchoString HTTP/1.1 Host: localhost:1489 User-Agent: Mozilla/5.0 Accept: text/html Content-Type: application/json; Content-Length: 27 ...

XML, JSON, SOAP, AtomPub ...

HTTP Communication

Headers

Data

Verb URL

Page 10: The Full Power of ASP.NET Web API

POST SimpleService.asmx/EchoString HTTP/1.1 Host: localhost:1489 User-Agent: Mozilla/5.0 Accept: text/html,application/xhtml+xml Content-Type: application/json; Content-Length: 27 ...

JSON vs. SOAP

{"Age":37,"FirstName":"Eyal",

"ID":"123", "LastName":"Vardi“ }

Headers

Data

Verb URL

<Envelope> <Header> <!–- Headers --> <!-- Protocol's & Polices --> </Header> <Body> <!– XML Data --> </Body> </Envelope>

Page 11: The Full Power of ASP.NET Web API

Protocols & Formats

BL’s

Interfaces

Services

WCF

WCF Data

Services

ASMX MVC Web API SignalR

B B B B B

SOAP REST OData JSON POX ??

Page 12: The Full Power of ASP.NET Web API

ASP.NET Web API vs. ASP.NET MVC

Validation

Help Page

Security

Routing

Self-Hosting

Dependency Injection

Filters

Content Negotiation

RESTful APIHTTP Message Handlers

Media Formatter

OData

Testing

Page 13: The Full Power of ASP.NET Web API

MICROSOFT CONFIDENTIAL – INTERNAL ONLY

GET /en/html/dummy.php?name=MyName&married=not+single &male=yes HTTP/1.1Host: www.explainth.atUser-Agent: Mozilla/5.0 (Windows;en-GB; rv:1.8.0.11) Gecko/20070312 Firefox/1.5.0.11Accept: text/xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5Accept-Language: en-gb,en;q=0.5Accept-Encoding: gzip,deflateAccept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7Keep-Alive: 300Connection: keep-aliveReferer: http://www.explainth.at/en/misc/httpreq.shtml

Embrace HTTP

Page 14: The Full Power of ASP.NET Web API

ASP.NET Ecosystem

ASP.NET

SignalR

Services

SPAMVCWebForms

WebAPI

Sites

Page 15: The Full Power of ASP.NET Web API

ASP.NET Web API (Hello World)

Page 16: The Full Power of ASP.NET Web API

REpresentational State Transfer

Page 17: The Full Power of ASP.NET Web API

REST is..

“An architectural style for building distributed

hypermedia systems.”

Page 18: The Full Power of ASP.NET Web API

WSDL vs. RESTWSDL description of a web services types defined in <xsd:schema>simple messages parts of messages

interface specification operations of an interface input message output message

binding of interface to protocols & encoding description of the binding for each operation

service descriptionURI and binding to port

<definitions> <types></types> <message> <part></part> </message> <portType> <operation> <input> <output> </operation> </portType> <binding> <operation> </binding> <service> <port> </service></definitions>

Page 19: The Full Power of ASP.NET Web API

From RPC to REST

* From ASP.NET MVC 4 and the Web API: Building a REST Service from Start to Finish book

Page 20: The Full Power of ASP.NET Web API

URIs and Resources (Routing)

* From ASP.NET MVC 4 and the Web API: Building a REST Service from Start to Finish book

Page 21: The Full Power of ASP.NET Web API

HTTP Verbs

* From ASP.NET MVC 4 and the Web API: Building a REST Service from Start to Finish book

Page 22: The Full Power of ASP.NET Web API

“Hypermedia as the engine of application state” (HATEOAS)<?xml version="1.0" encoding="utf-8"?><Tasks>

<TaskInfo Id="1234" Status="Active" > <link rel="self" href="/api/tasks/1234" method="GET" /> <link rel="users" href="/api/tasks/1234/users" method="GET" /> <link rel="history" href="/api/tasks/1234/history" method="GET" /> <link rel="complete" href="/api/tasks/1234" method="DELETE" /> <link rel="update" href="/api/tasks/1234" method="PUT" /> </TaskInfo>

<TaskInfo Id="0987" Status="Completed" > <link rel="self" href="/api/tasks/0987" method="GET" /> <link rel="users" href="/api/tasks/0987/users" method="GET" /> <link rel="history" href="/api/tasks/0987/history" method="GET" /> <link rel="reopen" href="/api/tasks/0987" method="PUT" /> </TaskInfo>

</Tasks>

/api/tasks

Page 23: The Full Power of ASP.NET Web API

“Hypermedia as the engine of application state” (HATEOAS)

* From ASP.NET MVC 4 and the Web API: Building a REST Service from Start to Finish book

Page 24: The Full Power of ASP.NET Web API

The Advantages of REST over SOAP Simpler API for CRUD

Standardize Development methodology

Wide industry adoption, Ease of use

Smaller payloads than SOAP

Testability

Page 25: The Full Power of ASP.NET Web API

RESTful

Page 26: The Full Power of ASP.NET Web API

OData OData is a standardized protocol for

creating and consuming data APIs.

Page 27: The Full Power of ASP.NET Web API

Query string options $Expand

/Customers(ALFKI)?$expand=Orders.Employees

$Orderby /Customers?$orderby=City desc,

CompanyName

$Skip /Customers?$skip=10

$Top /Orders?$orderby=TotalDue&$top=5

$Filter /Customers?$filter=City eq ‘London’

Page 28: The Full Power of ASP.NET Web API

LINQ to URI /ds.svc/Customers

from c in Customers select c

/ds.svc/Customers('ALFKI') ( from c in Customers

where c.keyProperty == "ALFKI" select c ).First()

/ds.svc/Customers('ALFKI')/Address ( from c in Customers

where c.keyProperty == "ALFKI" select c.Address ).First()

/ds.svc/Customers('ALFKI')/Orders from c in Customers

from c2 in c.Orders where c.keyProperty == "ALFKI" select c2

Page 29: The Full Power of ASP.NET Web API

OData

Page 30: The Full Power of ASP.NET Web API

Help Page  ASP.NET and Web Tools 2012.2 Update

integrates help pages into the Web API project template.

Web API generated dynamically, using the IApiExplorer interface.

Adding a simple Test Client to ASP.NET Web API Help Page

Page 31: The Full Power of ASP.NET Web API

Help Page

Page 32: The Full Power of ASP.NET Web API

Format & Model Binding

Page 33: The Full Power of ASP.NET Web API

Format & Model Binding

Serialization

Validation

DeserializationResponse

Controller / Action

Request HTTP/1.1 200 OK Content-Length: 95267 Content-Type: text/html | application/json Accept: text/html, application/xhtml+xml, application/xml

Page 34: The Full Power of ASP.NET Web API

Media Formatters (Serialization) A media-type formatter is an object that

can: Read CLR objects from an HTTP message body.

Write CLR objects into an HTTP message body.

GlobalConfiguration.Configuration.Formatters

Page 35: The Full Power of ASP.NET Web API

JSON Media-Type Formatter

Json.NET or DataContractJsonSerializer?

var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter; json.UseDataContractJsonSerializer = true;

Page 36: The Full Power of ASP.NET Web API

JSON Media-Type Formatter

What Gets Serialized? "opt-out" approach

"opt-in" approach

public class Product{ public string Name { get; set; } public decimal Price { get; set; } [JsonIgnore] public int ProductCode { get; set; }}

[DataContract] public class Product{ [DataMember] public string Name { get; set; } [DataMember] public decimal Price { get; set; } // omitted by default public int ProductCode { get; set; }}

Page 37: The Full Power of ASP.NET Web API

JSON Media-Type Formatter

Anonymous and Weakly-Typed Objects

public object Get() { return new { Name = "Alice", Age = 23, Pets = new List<string> { "Fido", "Polly", "Spot" } }; }

public void Post(JObject person) { string name = person["Name"].ToString(); int age = person["Age"].ToObject<int>(); }

Page 38: The Full Power of ASP.NET Web API

XML Media-Type Formatter

DataContractSerializer or XmlSerializer?

var xml = GlobalConfiguration.Configuration.Formatters.XmlFormatter; xml.UseXmlSerializer = true;

The XmlSerializer class supports a narrower set of types than DataContractSerializer, but gives more control over the resulting XML. Consider using XmlSerializer if you need to match an existing XML schema.

Page 39: The Full Power of ASP.NET Web API

Custom Media Formatters Derives from BufferedMediaTypeFormater.

In the constructor, add the media types that the formatter supports.

To indicate which types the formatter can serialize, Override the methods: CanWriteType CanReadType WriteToStream

Use the Formatters property on the HttpConfiguration object to add a custom media type formatter.

Page 40: The Full Power of ASP.NET Web API

Custom Media Formatters

Page 41: The Full Power of ASP.NET Web API

Content Negotiation The primary mechanism for content

negotiation in HTTP are these request headers: Accept Accept-Charset Accept-Encoding Accept-Language

“the process of selecting the best representation for a given response when there are multiple representations available.” 

Page 42: The Full Power of ASP.NET Web API

Content Negotiation Samplepublic HttpResponseMessage GetProduct(int id){ var product = new Product() { Id = id, Name = "Gizmo", Price = 1.99M };

IContentNegotiator negotiator = this.Configuration .Services.GetContentNegotiator();

ContentNegotiationResult result = negotiator.Negotiate( typeof(Product), this.Request, this.Configuration.Formatters);

if (result == null) { var response = new HttpResponseMessage(HttpStatusCode.NotAcceptable); throw new HttpResponseException(response)); }

return new HttpResponseMessage() { Content = new ObjectContent<Product>( product, // What we are serializing result.Formatter, // The media formatter result.MediaType.MediaType // The MIME type ) };}

Page 43: The Full Power of ASP.NET Web API

Validations

Page 44: The Full Power of ASP.NET Web API

Model Data Annotations Specify validation for individual fields in

the data model. System.ComponentModel.DataAnnotations

Provide both client and server validation checks with no additional coding required by you.

public class ProductMD{    [StringLength(50), Required]    public string Name { get; set; }                            [Range(0, 9999)]    public int Weight { get; set; }}

Page 45: The Full Power of ASP.NET Web API

Validation Attributes Metadata Validation Attributes:

[Required] [Exclude] [DataType] [Range]

[MetadataTypeAttribute( typeof( Employee.EmployeeMetadata ) )]public partial class Employee} internal sealed class EmployeeMetadata } [StringLength(60)] [RoundtripOriginal] public string AddressLine { get; set; } }}

[StringLength(60)] [RegularExpression

] [AllowHtml] [Compare]

Page 46: The Full Power of ASP.NET Web API

Web API Validation

public class ProductsController : ApiController{ public HttpResponseMessage Post(Product product) { if ( ModelState.IsValid ) { // Do something with the product (not shown). return new HttpResponseMessage(HttpStatusCode.OK); } else { return new HttpResponseMessage(HttpStatusCode.BadRequest); } }}

Page 47: The Full Power of ASP.NET Web API

Custom Validation public class ModelValidationFilterAttribute : ActionFilterAttribute { public override void OnActionExecuting(HttpActionContext actionContext) { if (actionContext.ModelState.IsValid == false) { // Return the validation errors in the response body. var errors = new Dictionary<string, IEnumerable<string>>(); foreach ( var keyValue in actionContext.ModelState ) { errors[keyValue.Key] = keyValue.Value.Errors.Select(e => e.ErrorMessage); } actionContext.Response = actionContext .Request .CreateResponse(HttpStatusCode.BadRequest, errors); } } }

Page 48: The Full Power of ASP.NET Web API

public class ModelValidationFilterAttribute : ActionFilterAttribute { public override void OnActionExecuting(HttpActionContext actionContext) { if (actionContext.ModelState.IsValid == false) { // Return the validation errors in the response body. var errors = new Dictionary<string, IEnumerable<string>>(); foreach ( var keyValue in actionContext.ModelState ) { errors[keyValue.Key] = keyValue.Value.Errors.Select(e => e.ErrorMessage); } actionContext.Response = actionContext .Request .CreateResponse(HttpStatusCode.BadRequest, errors); } } }

Custom Validation

protected void Application_Start() { // ... GlobalConfiguration .Configuration .Filters.Add(new ModelValidationFilterAttribute()); }

Page 49: The Full Power of ASP.NET Web API

Custom Validation

Page 50: The Full Power of ASP.NET Web API

Server-Side Message Handlers The Web API pipeline uses some built-in

message handlers: HttpServer gets the request from the host.

HttpRoutingDispatcher dispatches the request based on the route.

HttpControllerDispatcher sends the request to a Web API controller.

You can add custom handlers to the pipeline.

Page 51: The Full Power of ASP.NET Web API

Custom Message Handlers

Page 52: The Full Power of ASP.NET Web API

Self-Hosting

var config = new HttpSelfHostConfiguration("http://localhost:8080");

config.Routes.MapHttpRoute(    "API Default", "api/{controller}/{id}",     new { id = RouteParameter.Optional });

using (HttpSelfHostServer server = new HttpSelfHostServer(config)) {    server.OpenAsync().Wait();    Console.ReadLine(); }

Page 53: The Full Power of ASP.NET Web API
Page 54: The Full Power of ASP.NET Web API

Client Side

Page 55: The Full Power of ASP.NET Web API

Client Side Frameworks for SPA

Page 57: The Full Power of ASP.NET Web API

www.js-il.com

18.6.13

Page 58: The Full Power of ASP.NET Web API

Thankseyalvardi.wordpress.com

Eyal VardiCEO E4D Solutions LTDMicrosoft MVP Visual C#blog: www.e4d.co.il