Web API is a very popular framework nowadays, which helps us to build HTTP services. It is purely a restful service.
REST stands for Representational State Transfer and it is an architectural pattern to create an API that uses HTTP as its underlying communication method.
We are using Web API, generally for CRUD operations as, listed below:
- GET – To get the data from the Server
- POST- To post the data on the Server
- PUT- To updatde the data on the Server
- DELETE- To delete data on the Server
If we are not using API, then we have to write our business logic for every device like Android, Mac, Windows and Desktop etc. We are using Web API. We can write one business logic for every device.
Image Source: msdn.microsof.com
There are several advantages of using Web API
- Content Negotiation
- OData (Queryable attribute)
- Model Validation
- Ability to self host outside of IIS
- Full support for routes/routing
It means that the Server can return value, based on the client’s request, which media type formatter is going to be used to return an API response.
If a client request is Content-type: application/json, it will get the data in JSON format and if the client request is Content-type: application/xml, he will get data in an XML format.
OData (Queryable attribute)
OData stands for Open Data Protocol. It is used in the query string of the requested URI. OData queries can contain one or more query option.
Web API supports the $expand, $filter, $inlinecount, $orderby, $select, $skip, $top OData query options:
A very useful feature of Web API is a filter. We can define our own custom filters, as per our business need. Action filters are used for action level validation whereas Authentication Filter is used for authentication for the controller or specific action etc.
We can validate our model, before going for further processing and we can handle the validation errors also, in Web API, with the help of data annotations.
Ability to self host outside of IIS
Yes, It is possible to host the Web API, outside IIS.
Full support for routes/routing
Routing is the way of calling an action, using URL. Web API is fully supporting routing. We have different types of routing in Web API like Global routing and Attribute Routing. If you want to know more about routing, please go through my previous article about Routing In Web API.
We have many features in Web API. We will discuss more in the next article.