20
REST (RESTful) API

RESTful API and ASP.NET

Embed Size (px)

Citation preview

Page 1: RESTful API and ASP.NET

REST (RESTful) API

Page 2: RESTful API and ASP.NET

What is REST?

Page 3: RESTful API and ASP.NET

The five key principles are:• Give every “thing” an ID• Link things together• Use standard methods• Resources with multiple representations• Communicate statelessly

Page 4: RESTful API and ASP.NET

HTTP methods

Page 5: RESTful API and ASP.NET

Without REST

Page 6: RESTful API and ASP.NET

With REST

Page 7: RESTful API and ASP.NET

Build simple RESTful API

• /getAllMembers• /getAllCertificatedMembers• /getMember/1• /newMember• /saveMember

It’s not OK

Page 8: RESTful API and ASP.NET

It’s OK• POST /Members — Creation of a new member• GET /Members — A list of all members in the system• PUT /Members — An update to the all members • DELETE /Members — Delete all members• POST /Members/12345 — Return error (member 12345

are created)• GET /Members/12345 — Show member with ID 12345• PUT /Members/12345 — Edit member with ID12345• DELETE /Members/12345 — Delete

Page 9: RESTful API and ASP.NET

Hide the difficult method of the mark “?”

• GET /members?age=20&city=VN&location=park• POST /members?age=25&city=VN

Page 10: RESTful API and ASP.NET

Support for multiple formatsLike this :• /members.json• /members/1234.json

Or support multiple type of clients :• /members.ios• /members/1234.web

Page 11: RESTful API and ASP.NET

ASP.NET WEB API

Page 12: RESTful API and ASP.NET

Controller vs ApiControllerController ApiController

Page 13: RESTful API and ASP.NET

Controller vs ApiController

MVC Controller Default Route

API Controller Default Route

Page 14: RESTful API and ASP.NET

Asp.Net Web API VS Asp.Net MVC

Asp.Net Web API• to create HTTP services with

easy way that returns only data• returning data in particular

format like JSON,XML• the request are mapped to the

actions based on HTTP verbs

Asp.Net MVC• to create web applications that

returns both views and data• only return data in JSON format• it is mapped to actions name.

Page 15: RESTful API and ASP.NET

Web API Parameter Bindings• From Query-String: Using Model Binding• From Body: Using different data formats like JSON, XML.

Page 16: RESTful API and ASP.NET

Routing

Page 17: RESTful API and ASP.NET

URL: localhost:3333/test/users/11/tweets

Page 18: RESTful API and ASP.NET
Page 19: RESTful API and ASP.NET

Status Code

Page 20: RESTful API and ASP.NET