WebAPI is used to create REST based HTTP Services which are able to reach different types of clients.Today web clients are not restricted to just web browsers ,lot of different clients are being used to access the data over the internet.Clients such as mobile devices and tablets commonly access the data over the internet.For such broader category of clients ,which needs to access the information over HTTP, some technology specific to the HTTP is required which is what WebAPI provides.
WebAPI is used to create REST-ful web services.As WebAPI is all about creating REST based HTTP Services lets first understand what is REST here.
REST stands for ‘Representational State Transfer’ and is a pattern used to define the API using the HTTP.API means application programming interface and in the case of web applications usually means web services which are used to perform the CRUD operations on the data and may also contain some application logic.To perform CRUD operations such API usually connects to a database using a data access technology such as Entity Framework.
Resource The important thing in a REST services is that the resource is represented using an address or URL.These resources typically represents the data or business entities.
Verb This indicates what operation we want to perform on the Resource.There are different HTTP verbs which can be used to perform the different operations
POST For inserting a new entity
GET For fetching an entity or many entities
PUT For updating a specific entity
DELETE For deleting the entity
So for example we can represent a customer entity at a specific address and then can perform CRUD operations on it by using the different HTTP methods such as GET or POST.
WHY to use WebAPI instead of web services or WCF services?
One important question is why should we use WebAPI when we already have Web Services and WCF available for creating the services.There are few things worth mentioning
Web Services can be used to create services which uses SOAP over the HTTP protocol.Web services or WCF may not be not be suitable in the scenarios such as :
- Today there are different varieties of clients and using web services is not a good option when we consider these devices.Web services uses the SOAP message protocol for exchanging information.SOAP messages contains lots of information related to the request and also uses wsdl which is definitely an overhead when we consider the bandwidth available to these devices.
- SOAP request usually uses the POST method for transferring the message irrespective of the operation being performed.The message include details about the method such as the argument.In contrast REST uses different verbs such as POST or GET for performing different operations.This is closer to the true nature of the HTTP protocol.
- WCF service can communicate with the client using the different protocols ,such as HTTP or TCP.But if we need to communicate with different types of clients ,such as mobile apps using only the HTTP protocol then using WCF may not provide any benefit.Rather there is an overhead for configuring the WCF service.We need to configure things like binding,address and the contracts even for simple services.
- WCF services provides features such as Transactions and Reliable sessions which is not what we always require for our HTTP services
- We can create REST services using WCF also but it is not a very easy process.
Some of the features provided by the ASP.NET WebAPI are
- There are two main components used in ASP.NET WebAPI
HttpRequestMessage
HttpResponseMessage These objects allows the developer to directly work with the request and response in an object oriented way without being bothered with the low level request and response details. - Content negotiation means that the client and server agrees to the format of the information being interchanged ,this is done by using the accept HTTP header.For example to specify XML as the format of the information we can use the following accept: application/xml
- Flexible hosting options WebAPI can be hosted using the following 1)IIS Service hosting will be provided by IIS 2)SelfHosted we can host WebAPI service in a console application or in Windows service
- Stateless nature of web server One reason why REST services provides benefit is since the STATE information about an entity travels with the request and not maintained on the web server,the web server can support much more clients.In other words the web server is more scalable now.
- No dependency on System.Web Unlike ASP.NET WebForms and MVC WebAPI is not dependent on the System.Web assembly.