![Page 1: NET UY Meetup 5 - REST in peace with Web API by Fabian Alves](https://reader036.vdocuments.us/reader036/viewer/2022082915/547eef79b4af9fc6108b479b/html5/thumbnails/1.jpg)
Web Api
![Page 2: NET UY Meetup 5 - REST in peace with Web API by Fabian Alves](https://reader036.vdocuments.us/reader036/viewer/2022082915/547eef79b4af9fc6108b479b/html5/thumbnails/2.jpg)
Preguntas
• WebAPI• REST• MVC• WCF
![Page 3: NET UY Meetup 5 - REST in peace with Web API by Fabian Alves](https://reader036.vdocuments.us/reader036/viewer/2022082915/547eef79b4af9fc6108b479b/html5/thumbnails/3.jpg)
Que es Web Api
• Es un API que permite implementar servicios HTTP• Particularmente se utiliza para servicios web basados en la
arquitectura REST • Es una librería .Net que permite implementar servicios RESTful
en .Net• MVC es para retornar HTML, trabajar con forms y browsers – WebAPI
es para crear servicios y retornar json / xml• Es ideal para integrar dispositivos móviles
![Page 4: NET UY Meetup 5 - REST in peace with Web API by Fabian Alves](https://reader036.vdocuments.us/reader036/viewer/2022082915/547eef79b4af9fc6108b479b/html5/thumbnails/4.jpg)
Que es REST: Representational State Transfer• Es un estilo de arquitectura.• Orientado a recursos (sustantivos)• Utiliza las URIs para identificar recursos y los verbos HTTP para
manipularlos• GET, PUT, POST, DELETE
• La respuesta es via HTTP response codes estandars• 200 OK, 201 CREATED, 401 UNAUTHORIZED, ETC.
• El URI o el mensaje de request no incluye un verbo.• Hypermedia: el request a un recurso devuelve los URIs para acceder o
manipular otros recursos
![Page 5: NET UY Meetup 5 - REST in peace with Web API by Fabian Alves](https://reader036.vdocuments.us/reader036/viewer/2022082915/547eef79b4af9fc6108b479b/html5/thumbnails/5.jpg)
RESTful Services
• NO es RESTful• http://server/hrapp/getemployee?id=12345 • http://server/hrapp/employee?id=12345&action=GET • http://server/hrapp/employee and the request message determines the action to be carried out.
• Implementado con RESTful• http://server/hrapp/employees/12345• HTTP POST crea el employee 12345• HTTP GET obtiene el employee 12345• HTTP PUT modifica el employee 12345• HTTP DELETE borra el employee• Los datos vuelven codificados en JSON / XML• Los errores se devuelven con status code de HTTP (200 ok, 401 not authorized, 404 not-found,
etc)
![Page 6: NET UY Meetup 5 - REST in peace with Web API by Fabian Alves](https://reader036.vdocuments.us/reader036/viewer/2022082915/547eef79b4af9fc6108b479b/html5/thumbnails/6.jpg)
Como crear un Web API
• Crear un proyecto MVC4 usando el template Web API• Crear un controlador (controller) • Deriva de ApiController
• Agregar acciones a la clase controller• Se puede implementar WebAPI en cualquier tipo de proyecto
(console, self-host, etc)
![Page 7: NET UY Meetup 5 - REST in peace with Web API by Fabian Alves](https://reader036.vdocuments.us/reader036/viewer/2022082915/547eef79b4af9fc6108b479b/html5/thumbnails/7.jpg)
Routing en Web API
• Se pueden utilizar los nombres de los controladores y de las acciones para routear Web API requests• O también se pueden utilizar atributos para controlar el mapeo de los
requests a acciones en los controladores• HttpGet, HttpPut, HttpPost, HttpDelete• AcceptVerbs• ActionName
![Page 8: NET UY Meetup 5 - REST in peace with Web API by Fabian Alves](https://reader036.vdocuments.us/reader036/viewer/2022082915/547eef79b4af9fc6108b479b/html5/thumbnails/8.jpg)
Formatos de respuesta
• WebAPI puede devolver JSON o XML • WebAPI permite que el cliente especifique el formato de respuesta en
el HTTP header• Se pueden crear “MediaTypeFormatters” para devolver distintos tipos
de datos – por ejemplo una imagen
![Page 9: NET UY Meetup 5 - REST in peace with Web API by Fabian Alves](https://reader036.vdocuments.us/reader036/viewer/2022082915/547eef79b4af9fc6108b479b/html5/thumbnails/9.jpg)
JSON• Request SentPOST /api/employees HTTP/1.1Content-Type: application/json; charset=utf-8Content-Length: 49{"Name":"John Q Law", "Department":"Enforcement"}
• Response ReceivedHTTP/1.1 200 OKContent-Type: application/json; charset=utf-8{"Department":"Enforcement","Id":"123","Name":"John Q Law"}
![Page 10: NET UY Meetup 5 - REST in peace with Web API by Fabian Alves](https://reader036.vdocuments.us/reader036/viewer/2022082915/547eef79b4af9fc6108b479b/html5/thumbnails/10.jpg)
XML• Request SentPOST /api/employees HTTP/1.1Content-Type: application/xml; charset=utf-8Content-Length: 80<Employee><Name>John Q Law</Name><Department>Enforcement</Department></Employee>
• Response ReceivedHTTP/1.1 200 OKContent-Type: application/xml; charset=utf-8<?xml version="1.0" encoding="utf-8"?><Employee xmlns:xsi=http://www.w3.org/2001/XMLSchemainstance xmlns:xsd="http://www.w3.org/2001/XMLSchema"><Id>123</Id><Name>John Q Law</Name><Department>Enforcement</Department></Employee>
![Page 11: NET UY Meetup 5 - REST in peace with Web API by Fabian Alves](https://reader036.vdocuments.us/reader036/viewer/2022082915/547eef79b4af9fc6108b479b/html5/thumbnails/11.jpg)
WCF vs WebAPI
• WCF permite implementar un servicio sin definir el mecanismo de transporte, el formato de los paquetes, el protocolo, etc. • WCF es mas flexible y mas complejo.• WCF soporta HTTP, TCP, Namepipes (comunicación entre procesos),
etc. • WebAPI es para implementar HTTP con JSON / XML.• Si solo se va a necesitar HTTP con JSON / XML conviene usar WebAPI.• Si existe la posibilidad de que los servicios se expongan o consuman
de otra manera conviene WCF.
![Page 12: NET UY Meetup 5 - REST in peace with Web API by Fabian Alves](https://reader036.vdocuments.us/reader036/viewer/2022082915/547eef79b4af9fc6108b479b/html5/thumbnails/12.jpg)
Mas info
Documentaciónhttp://www.asp.net/web-api • Fiddlerhttp://fiddler2.com/home • Web API Test Client http://
blogs.msdn.com/b/yaohuang1/archive/2012/12/02/adding-a-simple-test-client-to-asp-net-web-api-help-page.aspx • Nuget Package “A simple Test Client to ASP.NET Web API”