REST and ASP.NET Web API (Milan)

Preview:

Citation preview

REST & ASP.NET Web API@JefClaes

Thanks to the sponsors

about/jefclaes

@jefclaes

http://jefclaes.be

REST

The acronym

REpresentational State Transfer

REST is NOT...

• ‘Webservices using JSON’

• A protocol• A design pattern

REST is..

“An architectural style for building distributed hypermedia systems.”

In theory...

REST isn’t limited to a single message protocol.

But in practice...

It’s all about HTTP.

So now you wonder..

Haven’t we been successfully building web services using SOAP and HTTP

for over 10 years ?

BASIC CONCEPTS

Resources

• An entity, item, or just a thing you want to expose.

• REST is Resource Oriented.

Resources

Resources

Resources

Resources

Identifiers

Something that identifies a resource.

ID: 1URI: http://batman.com/api/batresource/1

Representations

View on a resource’s state at an instant in time.

JSON:

{   "Id":"1",   "Description":"Batarang",   "Quantity":1}

XML:

<BatResource> <Id>1</Id> <Description> Batarang </Description> <Quantity>1</Quantity></BatResource>

Other:

• Images• CSV• Custom• ...

Verbs

Actions on a resource.

Create Read Update Delete

POST GET PUT DELETE

Hypermedia

HATEOAS: Hypermedia as the engine of application state

Linking your API together

{ "Id":"1", "Description":"Batarang", “Quantity":1, “Links": { "Rel":"Next","Href":"http://localhost:8080/api/batresource/2" }}

HypeRRRRRRRmedia

• Relations• Embedded resources• Reference data• Redistribution of effort• Reduction of payload size• Reflow• Restriction of functionality

ARCHITECTURAL VALUESArchitecture of the WEB

Scalability and performance

Yes, text-based, synchronous, request-response can be performant.• Stateless• Caching

Loose coupling

• No transactions• No state• No guarantees• HATEOAS• No specific technology stack

Consistency and Uniformity

Everybody knows how to use HTTP–Constraints–Well understood semantics

LEFT-OVERS

Richardson’s Maturity Model

Level 0: POX

Level 1: Resources

Level 2: HTTP verbs

Level 3: Hypermedia

ASP.NET WEBAPI

What?

.NET(4.0) HTTP framework for building RESTful services.

When?

• HTTP Services (WCF)

• AJAX back-ends

The server

Options

• WebHost– ASP.NET MVC: Click, click, click

• SelfHost

Starting

Mapping Web API to REST concepts

A resource

A resource

Identifiers

Identifiers

Verbs

Verbs

Representations

Representations

application/jsontext/xml

Representations

Hypermedia

Hypermedia

The Client

Package

Microsoft.AspNet.WebApi.Client

Verbs and asynchrony

Summary

• REST–Concepts–Architectual values– Left overs

• ASP.NET Web API– Server–Client

There is more

Slides and source on http://jefclaes.be

Extensibility?• Next session (Raffaele Rialdi)• http://github.com/JefClaes/aspnet-webapi-samples-tunisia

Please rate this sessionScan the code, go online, rate this session

Recommended