Top Banner
Building Service for Any Clients Lohith G N Telerik
20

GIDS13 - Building Service for Any Clients

Jan 14, 2015

Download

Technology

Lohith GN

My Great Indian Developer Summit 2013 talk on "Building Service for Any Clients using Web API". In this session i give a introduction to Web API and how to create a Web API.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: GIDS13 - Building Service for Any Clients

Building Service for Any Clients

Lohith G NTelerik

Page 2: GIDS13 - Building Service for Any Clients

ABOUT ME

• Developer Evangelist, Telerik India• Microsoft MVP

• @kashyapa• [email protected]

• www.Kashyapas.com• www.Telerikhelper.net

Page 3: GIDS13 - Building Service for Any Clients

Telerik at a Glance

• Established in 2002• Telerik is now a leading vendor of productivity tools & solutions• 11 global offices, 700+ people, 100,000+ loyal customers in over 90

countries• 880,000 Registered users in the Telerik Online Community • True global vendor – no vertical or geographical focus• Numerous business awards, hundreds of technology awards

““Deliver More Than Deliver More Than Expected”Expected”

Page 4: GIDS13 - Building Service for Any Clients

End to End Provider

Solutions for all aspects of Software Development

Automated Functional & Performance UI Testing

Unit Testing

Load/Stress Testing

Exploratry Testing

Testing

Requirements Gathering

Project Management

Defect Management

Team and Customer Collaboration

Planning

Multi-platform UI tools

Code quality and performance tools

Data access and reporting tools

Construction

Page 5: GIDS13 - Building Service for Any Clients

Telerik Product Portfolio

PlanPlan BuildBuild TestTest DeliverDeliverAGILE PROJECT AGILE PROJECT MANAGEMENTMANAGEMENT

DEVELOPER TOOLSDEVELOPER TOOLS QUALITY ASSURANCE QUALITY ASSURANCE TOOLSTOOLS

WEB PRESENCE WEB PRESENCE PLATFORMPLATFORM

Windows Phone*

Sitefinity

OpenAccess ORM

Silverlight

WinForms

Reporting

JustCode

WPF Controls

ASP.NET MVC

JustMock

ASP.NET AJAX*

JustDecompile

TeamPulse

Windows 8*

TestStudio

Page 6: GIDS13 - Building Service for Any Clients

Agenda

• How does ASP.NET Web API fit in?

• Introduction to Web API

• Consuming Web API from jQuery

• Consuming Web API from Windows 8

Page 7: GIDS13 - Building Service for Any Clients

Web API is part of ASP.NET

ASP.NET Core

Page 8: GIDS13 - Building Service for Any Clients

WHERE CAN YOU GET WEB API

Page 9: GIDS13 - Building Service for Any Clients

www.asp.net/web-api

Page 10: GIDS13 - Building Service for Any Clients

Building a read only Web API

Page 11: GIDS13 - Building Service for Any Clients

Sample Read-only Model and Controller

public class Personpublic class Person{{ public int Id { get; set; }public int Id { get; set; } public string Name { get; public string Name { get; set; }set; }}}

Step 1:Create a Model

public class PersonController : public class PersonController : ApiControllerApiController{{ List<Person> _people;List<Person> _people; public PersonController()public PersonController() {{ _people = new List<Person>();_people = new List<Person>(); _people.AddRange(new Person[]_people.AddRange(new Person[] {{ new Person { Id = 1, Name = new Person { Id = 1, Name = "Chuck Norris" },"Chuck Norris" }, new Person { Id = 2, Name = new Person { Id = 2, Name = "David Carradine" },"David Carradine" }, new Person { Id = 3, Name = new Person { Id = 3, Name = "Bruce Lee" }"Bruce Lee" } });}); }}}}

Step 2:Make an API Controller

Page 12: GIDS13 - Building Service for Any Clients

Read-only Controller Actions to return data

// GET /api/person// GET /api/personpublic IEnumerable<Person> Get()public IEnumerable<Person> Get(){{ return _people;return _people;}}

Step 3:Return everything

// GET /api/person/5// GET /api/person/5public Person Get(int id)public Person Get(int id){{ return _people.First(x => x.Id return _people.First(x => x.Id == id);== id);}}

Step 4:Return one item

Page 13: GIDS13 - Building Service for Any Clients

Routing a Web API Using Global.asax.cs

public static void public static void RegisterRoutes(RouteCollection RegisterRoutes(RouteCollection routes)routes){ { routes.MapHttpRoute(routes.MapHttpRoute( name: "DefaultApi",name: "DefaultApi", routeTemplate: routeTemplate: "api/{controller}/{id}","api/{controller}/{id}", defaults: new { id = defaults: new { id = RouteParameter.Optional }RouteParameter.Optional } ););}}

Routing:Familiar syntax, conventional approach

Page 14: GIDS13 - Building Service for Any Clients

Manipulating HTTP Responses

// GET /api/person/5// GET /api/person/5public HttpResponseMessage<Person> Get(int id)public HttpResponseMessage<Person> Get(int id){{ trytry {{ var person = _people.First(x => x.Id == var person = _people.First(x => x.Id == id);id);

return new HttpResponseMessage<Person>(return new HttpResponseMessage<Person>( person,person, HttpStatusCode.OKHttpStatusCode.OK );); }} catchcatch {{ return new return new HttpResponseMessage<Person>(HttpStatusCode.NotFounHttpResponseMessage<Person>(HttpStatusCode.NotFound);d); }}}}

ExampleFind a person and return it,but what happens if we don’t find a match?

Page 15: GIDS13 - Building Service for Any Clients

Manipulating HTTP Responses

A successful API call returns an HTTP OK and the JSON data

Page 16: GIDS13 - Building Service for Any Clients

Manipulating HTTP Responses

An unsuccessful API call returns an HTTP 404 (and no JSON)

Page 17: GIDS13 - Building Service for Any Clients

Making an API Updatable

Page 18: GIDS13 - Building Service for Any Clients

Posting Data to a Web API

public HttpResponseMessage Post(Person person)public HttpResponseMessage Post(Person person){{ person.Id = _people.Count + 1;person.Id = _people.Count + 1;

if (_people.Any(x => x.Id == person.Id))if (_people.Any(x => x.Id == person.Id)) return new return new HttpResponseMessage(HttpStatusCode.BadRequest);HttpResponseMessage(HttpStatusCode.BadRequest);

trytry {{ _people.Add(person);_people.Add(person); }} catchcatch {{ return new return new HttpResponseMessage(HttpStatusCode.BadRequest);HttpResponseMessage(HttpStatusCode.BadRequest); }}

return new return new HttpResponseMessage(HttpStatusCode.OK);HttpResponseMessage(HttpStatusCode.OK);}}

Use HTTP Post:Pass a Model

Page 19: GIDS13 - Building Service for Any Clients

Posting Data to a Web API

Page 20: GIDS13 - Building Service for Any Clients