Upload
lohith-gn
View
407
Download
1
Tags:
Embed Size (px)
DESCRIPTION
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.
Citation preview
Building Service for Any Clients
Lohith G NTelerik
ABOUT ME
• Developer Evangelist, Telerik India• Microsoft MVP
• @kashyapa• [email protected]
• www.Kashyapas.com• www.Telerikhelper.net
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”
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
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
Agenda
• How does ASP.NET Web API fit in?
• Introduction to Web API
• Consuming Web API from jQuery
• Consuming Web API from Windows 8
Web API is part of ASP.NET
ASP.NET Core
WHERE CAN YOU GET WEB API
www.asp.net/web-api
Building a read only Web API
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
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
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
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?
Manipulating HTTP Responses
A successful API call returns an HTTP OK and the JSON data
Manipulating HTTP Responses
An unsuccessful API call returns an HTTP 404 (and no JSON)
Making an API Updatable
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
Posting Data to a Web API