Telerik is a leading vendor of productivity tools and solutions with over 11 global offices, 700 employees, and 100,000 loyal customers. Their product portfolio includes tools for planning, building, testing, and delivering agile projects including developer tools, quality assurance tools, and web presence platforms. This presentation will cover how ASP.NET Web API fits into their offerings and provide an introduction to building a basic read-only and updatable Web API.
2. ABOUT ME
• Developer Evangelist, Telerik India
• Microsoft MVP
• @kashyapa
• Lohith.Nagaraj@telerik.com
• www.Kashyapas.com
• www.Telerikhelper.net
3. 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 ThanDeliver More Than
Expected”Expected”
4. 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
5. Telerik Product Portfolio
PlanPlan BuildBuild TestTest DeliverDeliver
AGILE PROJECTAGILE PROJECT
MANAGEMENTMANAGEMENT
DEVELOPER TOOLSDEVELOPER TOOLS QUALITY ASSURANCEQUALITY ASSURANCE
TOOLSTOOLS
WEB PRESENCEWEB 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
6. Agenda
• How does ASP.NET Web API fit in?
• Introduction to Web API
• Consuming Web API from jQuery
• Consuming Web API from Windows 8
11. 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; set; }public string Name { get; set; }
}}
Step 1:
Create a Model
public class PersonController : ApiControllerpublic class PersonController : ApiController
{{
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 = "Chucknew Person { Id = 1, Name = "Chuck
Norris" },Norris" },
new Person { Id = 2, Name = "Davidnew Person { Id = 2, Name = "David
Carradine" },Carradine" },
new Person { Id = 3, Name = "Brucenew Person { Id = 3, Name = "Bruce
Lee" }Lee" }
});});
}}
}}
Step 2:
Make an API Controller
12. Read-only Controller Actions to
return data
// GET /api/person// GET /api/person
public IEnumerable<Person> Get()public IEnumerable<Person> Get()
{{
return _people;return _people;
}}
Step 3:
Return everything
// GET /api/person/5// GET /api/person/5
public 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
13. Routing a Web API Using
Global.asax.cs
public static voidpublic static void
RegisterRoutes(RouteCollectionRegisterRoutes(RouteCollection
routes)routes)
{{
routes.MapHttpRoute(routes.MapHttpRoute(
name: "DefaultApi",name: "DefaultApi",
routeTemplate: "api/routeTemplate: "api/
{controller}/{id}",{controller}/{id}",
defaults: new { id =defaults: new { id =
RouteParameter.Optional }RouteParameter.Optional }
););
}}
Routing:
Familiar syntax,
conventional approach
14. Manipulating HTTP Responses
// GET /api/person/5// GET /api/person/5
public HttpResponseMessage<Person> Get(int id)public HttpResponseMessage<Person> Get(int id)
{{
trytry
{{
var person = _people.First(x => x.Id == id);var person = _people.First(x => x.Id == id);
return new HttpResponseMessage<Person>(return new HttpResponseMessage<Person>(
person,person,
HttpStatusCode.OKHttpStatusCode.OK
););
}}
catchcatch
{{
return newreturn new
HttpResponseMessage<Person>(HttpStatusCode.NotFound);HttpResponseMessage<Person>(HttpStatusCode.NotFound);
}}
}}
Example
Find a person and return it,
but what happens if we don’t
find a match?
18. 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 newreturn new
HttpResponseMessage(HttpStatusCode.BadRequest);HttpResponseMessage(HttpStatusCode.BadRequest);
trytry
{{
_people.Add(person);_people.Add(person);
}}
catchcatch
{{
return newreturn new
HttpResponseMessage(HttpStatusCode.BadRequest);HttpResponseMessage(HttpStatusCode.BadRequest);
}}
return new HttpResponseMessage(HttpStatusCode.OK);return new HttpResponseMessage(HttpStatusCode.OK);
}}
Use HTTP Post:
Pass a Model