Monthly Archives: October 2012

A detailed Introduction to ASP.NET Web API

This is a series of article on the Microsoft new framework called ASP.NET Web API which is released as part of the ASP.NET MVC 4.  ASP.NET Web API is the Microsoft’s recent solid answer for the HTTP oriented protocol services, in other words, REST services. Microsoft defines it as ASP.NET Web API is a framework that makes it easy to build HTTP services that reach a broad range of clients, including browsers and mobile devices. ASP.NET Web API is an ideal platform for building RESTful applications on the .NET Framework.

HTTP oriented RESTful services

HTTP oriented RESTful services are services that make use of HTTP verbs for its CRUD functionalities. HTTP GET verb for read/retrieve functionality, POST verb for Creation, PUT verb for updation, DELETE verb for removal.

For Example, if you are creating an online Bookstore application. You will use:

  1. HTTP GET verb for retrieving all customer details of the book store or detail of a particular customer.
  2. HTTP POST verb for creating any new customer when he/she signs up for your online book store.
  3. HTTP PUT verb for updating existing customer information like name, emailed, address or his/her subscription plan change.
  4. HTTP DELETE verb to remove the customer from the book store customer list when he/she terminates the subscription.  You can either remove the customer permanently or just put some flags and mark the customer as removed without removing his/her entire account from the application, well, that is internal detail as per the application requirements.

HTTP protocol oriented services have been in use for a few years already and is been a trendy way to implement web services (I mean services that run on the web, not the classic Web Services specifically as there is a major difference between HTTP service and classic Web Service). But really a great push towards HTTP services raised when hand-held devices and portable devices like smartphone, tablet evolved to a great extent as it used a rich internet applications with rich user interfaces.

The main driving forces of HTTP oriented services are:

  1. Hand-held portable devices like smart phones and tablets, these are the revolutionary devices that brought a whole new computing trend these days by brining lots and lots of apps with great user interface that needed huge dynamic data from its server.
  2. Browser based applications, applications like games, weather apps, personal apps etc., that run as embedded apps on the browser with rich user interface
  3. RIA applications/SPA (Single Page Applications) on the web needed dynamic data so frequently than plain html from its server and that data is rendered on the page using client technologies like JQuery, Knockout etc.
  4. Windows 8 brought a whole new trend of bringing desktop apps that run on the windows desktop like tablet apps. Those apps send/receive data over the net and those apps can be Microsoft’s own apps or third party apps installed from Microsoft store.

 ASP.NET Web APIs are different/better from classic Web Service and WCF:

Though these two frameworks can be used to develop services to support heterogeneous devices and platforms, their main differences are in their communication protocols. Classic Web Service uses SOAP and ASP.NET Web API uses HTTP as their respective communication protocol under the hood.

Diagram 1:  Heterogeneous devices possibly with various OS and applications communicating with SOAP, A classic Web Service scenario. 

Diagram 1:  Heterogeneous devices possibly with various OS and applications communicating with HTTP protocol, A ASP.NET WebAPI scenario. 

Advantages of ASP.NET Web API that uses Http protocol are:

  1. HTTP messages can be cached, firewall friendly and lightweight.
  2. They can be encrypted and can be processed by the processor in mobile devices.
  3.  Most of the devices in the world have capability to send and receive HTTP messages. So major reach.
  4. ASP.NET WebAPI provides many features like content negotiation and URI routing out of the box whereas in WCF you got to implement them using some plumbing.
  5. Full support for ASP.NET routing and filters
  6. Strong support of variety of output formats like JSON, XML and ATOM.
  7. Easy to unit test and provides self-hosting support.

Comparing ASP.NET WebAPI and Webforms and MVC:

  1. WebApi is designed especially to meet the current trend  of rich clients/applications that are running on web browser that are developed using client technologies like Jquery, knockout etc are consuming lightweight services over http protocol. In such cases webserver’s main role is not just about serving some html content but it’s really about accepting data and returning data in different format. You might have clients that are not just browser, it can be a mobile devices.
  2. But Webforms or MVC are mainly for consuming html. It can do other things like webapi, but for that you got to do a lot of plumbing work to make it happen. In other words, MVC is page/html oriented but webapi is API oriented.

Summary:

ASP.NET Web API is definitely a strong framework for building RESTful services compare to other existing framework like ASP.NET MVC and WCF. I strongly recommend anyone planning to use this technology for their service implementation that are targeting to global reach.

Let’s dive in some coding implementation of ASP.NET Web API in the next article of this series.

Next article in this series is here :

https://alagesann.com/2012/11/03/introductory-demo-on-asp-net-webapi/

Thanks for reading.

Advertisements