introduction to asp.net web api

18
® Building a Service Layer with ASP.NET Web API LOHITH G. N. DEV EVANGELIST, TELERIK [email protected]

Upload: lohith-gn

Post on 07-Nov-2014

2.314 views

Category:

Technology


9 download

DESCRIPTION

Quick introduction to Web API

TRANSCRIPT

Page 1: Introduction to asp.net web api

®

Building a Service Layer with ASP.NET Web API

LOHITH G. N.

DEV EVANGELIST, TELERIK

[email protected]

Page 2: Introduction to asp.net web api

Agenda

• How does ASP.NET Web API fit in?

• Introduction to Web API• Consuming Web API from

jQuery

Page 3: Introduction to asp.net web api

®

Web API is a part of ASP.NET

Caching

Modules Handlers

Intrinsics

Membership

Etc.

ASP.NET Core

MVCWeb Pages

Web Forms

Razor View Engine

MVC 4

HTML

Web API

Code

JSON

XML

Page 4: Introduction to asp.net web api

Where Can You Get Web API?

Page 5: Introduction to asp.net web api

®

Homepage: asp.net/web-api

Page 6: Introduction to asp.net web api

Building a Read Only Web API

Why?

Allow browser or other clients to easily retrieve information from your system

Page 7: Introduction to asp.net web api

®

Sample Read-only Model and Controller

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

Step 1:Create a Model

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

Step 2:Make an API Controller

Page 8: Introduction to asp.net web api

®

Read-only Controller Actions to return data

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

Step 3:Return everything

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

Step 4:Return one item

Page 9: Introduction to asp.net web api

®

Routing a Web API Using Global.asax.cs

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

Routing:Familiar syntax, conventional approach

Page 10: Introduction to asp.net web api

Manipulating HTTP Responses

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

return new HttpResponseMessage<Person>( person, HttpStatusCode.OK ); } catch { return new HttpResponseMessage<Person>(HttpStatusCode.NotFound); }}

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

Page 11: Introduction to asp.net web api

Manipulating HTTP ResponsesA successful API call returns an HTTP OK and the JSON data

Page 12: Introduction to asp.net web api

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

Page 13: Introduction to asp.net web api

Making an API Updatable

Why?

Allow clients to modify the state of the server

Page 14: Introduction to asp.net web api

®

Posting Data to a Web API

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

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

try { _people.Add(person); } catch { return new HttpResponseMessage(HttpStatusCode.BadRequest); }

return new HttpResponseMessage(HttpStatusCode.OK);}

Use HTTP Post:Pass a Model

Page 15: Introduction to asp.net web api

®

Posting Data to a Web API

Page 16: Introduction to asp.net web api

Introduction to ASP.NET Web API

demo

Page 17: Introduction to asp.net web api

®

Resources

Feedback and questions http://forums.dev.windows.com

www.asp.net

Page 18: Introduction to asp.net web api

®