What is Web API
API is an Application Programming Interface over the web which can be accessed by using HTTP protocols.
ASP.NET Web API is a framework for building web APIs, i.e., HTTP-based services on top of the ASP.NET Framework. The most common use of Web API is building RestFul Services. It supports different formats of response data. Built-in support for JSON, XML, BSON format.
ASP.NET Web API can be hosted in IIS, self-hosted, or other web server that supports .NET 4.0+. These services can be used by a broad range of clients, like:
- Browser
- Mobile App
- Desktop App
- IOTs (Internet Of Things) like securities system, electronic appliances which has IP address and they use internet services.
We can use Web APIs even for the services which are not RESTful.
Representational state transfer (RESTFULL) is an architectural pattern for creating services, which uses HTTP for communication. It is an architectural pattern which specifies the set of constraints which systems must follow.
Below are the constraints,
- Client server
This helps the separation of client side logic and server side logic. In this client sends request and server sends a response.
- Stateless
This says communication between client and server must be stateless; i.e. there will not be any information about the client to be stored on the server. Client request will contain all the information, which means every request will be treated independent new request.
- Cacheable
Caching stored data which is not going to change often. It avoids unnecessary processing which helps in improving the performance of system.
- Uniform Interface
It defines the interface between client and server. In this, each request tells with the help of HTTP Verbs what to do with the resources like Employee, Products, etc. Another important thing about Uniform Interface is Hyper Media as Engine of Application State (HATEOS), which means there will be set of Hyperlinks that tells whaht actions need to be performed on the resource for a request.
- Layered System
There are many layers between the client and the server. These are called intermediaries and can be added at various points in the request-response path without changing the interfaces between components, where they do things to passed messages such as translation or improving performance with caching. Intermediaries include proxies and gateways.
- Code on Demand
Code on demand is an optional constraint. It allows a client to download and execute code from a server.
Now, let’s discuss the difference between WCF and Web API.
WCF | WEB API |
We can use for creating RESTFUL Services, however, it requires a lot of configuration to achieve the same. | It is specially created for REST. |
WCF is more suited for the services that are transport protocol independent like HTTP, TCP, MSMQ. | Web API is only suitable for Http. |
Now, let’s see how to create Asp.Net Web API Project.
- File - New Project - Select Asp.Net Web Application and add the name of the project, as shown below.
- Now, select Web API, as shown below.
- Now, go to Solution Explorer, you will get the default template, as shown below.
Now, in the above image, ValuesController is a WebAPI controller. It inherits System.Web.Http.ApiController class which is similar to ASP.NET MVC Controller, however, it handles HTTP requests.
It is created in Controller folder. All the public methods which are present in this are called as action methods.
Now, run the application and access the below URL http://localhost:XXX /api/values.
Now, in the next article, we will be discussing HTTP Verbs and their implementation in Web API.