spca2014 hillier build your_own_rest_service

31

Upload: nccomms

Post on 02-Jul-2015

262 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Spca2014 hillier build your_own_rest_service
Page 2: Spca2014 hillier build your_own_rest_service

Build Your Own REST Service with Web API 2

Scot Hillier

MVP

Scot Hillier Technical Solutions, LLC

Page 3: Spca2014 hillier build your_own_rest_service

[email protected]

@ScotHillier

Page 4: Spca2014 hillier build your_own_rest_service
Page 5: Spca2014 hillier build your_own_rest_service
Page 6: Spca2014 hillier build your_own_rest_service
Page 7: Spca2014 hillier build your_own_rest_service
Page 8: Spca2014 hillier build your_own_rest_service
Page 9: Spca2014 hillier build your_own_rest_service

public IEnumerable<string> Get() {}

public string Get(int id) {}

public void Post([FromBody]string value){}

public void Put(int id, [FromBody]string value){}

public void Delete(int id){}

public class ValuesController : ApiController

Page 10: Spca2014 hillier build your_own_rest_service

config.Routes.MapHttpRoute(name: "DefaultApi",routeTemplate: "api/{controller}/{id}",defaults: new { id = RouteParameter.Optional }

);

Page 11: Spca2014 hillier build your_own_rest_service

"application/json"

"application/xml"

public IQueryable<string> Get(){

var d = new List<string>() {"a", "b" };return d.AsQueryable();

}

public HttpResponseMessage Get(int id){return Request.CreateResponse<string>(HttpStatusCode.OK, data[id - 1]);

}

Page 12: Spca2014 hillier build your_own_rest_service
Page 13: Spca2014 hillier build your_own_rest_service
Page 14: Spca2014 hillier build your_own_rest_service
Page 15: Spca2014 hillier build your_own_rest_service
Page 16: Spca2014 hillier build your_own_rest_service

<Property Name="Id" Type="Edm.Guid" Nullable="false"/><Property Name="Title" Type="Edm.String"/><Property Name="TreeViewEnabled" Type="Edm.Boolean" Nullable="false"/><Property Name="UIVersion" Type="Edm.Int32" Nullable="false"/>

Page 17: Spca2014 hillier build your_own_rest_service

<EntityType Name="Site"><EntityType Name="Web" BaseType="SP.SecurableObject"><EntityType Name="List" BaseType="SP.SecurableObject"><EntityType Name="ListItem" BaseType="SP.SecurableObject" OpenType="true">

<Key><PropertyRef Name="Id"/></Key>

<NavigationProperty Name="RootWeb" …

Page 18: Spca2014 hillier build your_own_rest_service
Page 19: Spca2014 hillier build your_own_rest_service

ApiController

IQueryable

public class ContactsController : ODataController

Page 20: Spca2014 hillier build your_own_rest_service

ODataConventionModelBuilder builder = new ODataConventionModelBuilder();builder.EntitySet<Contact>("Contacts");builder.EntitySet<Company>("Companies");config.Routes.MapODataRoute("odata", "odata", builder.GetEdmModel());

Page 21: Spca2014 hillier build your_own_rest_service
Page 22: Spca2014 hillier build your_own_rest_service
Page 23: Spca2014 hillier build your_own_rest_service
Page 24: Spca2014 hillier build your_own_rest_service

EnableCors

Page 25: Spca2014 hillier build your_own_rest_service

Pre-flight request

Request Headers

Response Headers

Page 26: Spca2014 hillier build your_own_rest_service
Page 27: Spca2014 hillier build your_own_rest_service
Page 28: Spca2014 hillier build your_own_rest_service
Page 29: Spca2014 hillier build your_own_rest_service
Page 30: Spca2014 hillier build your_own_rest_service
Page 31: Spca2014 hillier build your_own_rest_service