What is Web API?

Reading Time: 5 minutes

What is an API?

Before we answer the question “What is Web API?”, let’s see what is an API in general.

API stands for (Application Programming Interface) and is an interface allows developers to access specific features or data in a certain application, operating system or other services.

Web API can be accessed using HTTP protocol. We can build a Web API using different technologies such as Java, .NET or other languages. For example, Facebook’s REST APIs provide programmatic access to read and write data in our own application, it can be a web, desktop or mobile application.

Example: we can use the Facebook API to Get a list of likes or post to our page programmatically.

ASP.NET Web API

Works in the same way as ASP.NET MVC web application except that it sends data as a response (usually JSON) instead of HTML view. It is like a web service or WCF service but the exception is that it only supports HTTP protocol.

Web API Features

  • It supports CRUD Actions since it works with HTTP verbs GET, POST, PUT and DELETE.
  • ASP.NET Web API is an ideal platform for building RESTful services.
  • Responses are formatted by Web API’s MediaTypeFormatter into JSON, XML or whatever format you want to add as a MediaTypeFormatter.
  • It can be hosted in IIS, Self-hosted or other web servers that support .NET 4.0+.
  • It also supports the MVC features such as routing, controllers, action results, filter, model binders, IOC container or dependency injection that makes it more simple and robust.
  • ASP.NET Web API is built on top of ASP.NET.

ASP.NET Web API Versions:

Web API Version  .NET Framework Works with
Web API 1.0 .NET Framework 4.0 ASP.NET MVC 4
Web API 2 – Current .NET Framework 4.5 ASP.NET MVC 5

 

Example:

Some companies hold data about geolocation (IP, countries… data), You can use their website to enter a certain IP and you get information about the location (city, country, codes…) of that IP.

If these companies want developers to access their databases through their code and not the website, then what is the solution. SImply it’s Web API.

They can develop their own API on top of their web application that allows developers to access their data and services through it.

H-educate webapi

 

ASP.NET Web API vs WCF:

Uses web.config and attributes to configure a service. Supports RESTful services but with limitations.

Web API WCF
Open source and ships with .NET framework. Ships with .NET framework
Supports only HTTP protocol. Supports HTTP, TCP, UDP and custom transport protocol.
Maps HTTP verbs to methods Uses attributes based programming model.
Uses routing and controller concept similar to ASP.NET MVC. Uses Service, Operation, and Data contracts.
Does not support Reliable Messaging and transaction. Supports Reliable Messaging and Transactions.
Web API can be configured using HttpConfiguration class but not in web.config. Uses web.config and attributes to configure a service.
Ideal for building RESTful services. Supports RESTful services but with limitations.

 

To Get started there is good Getting started tutorial from Microsoft, check the link below:

Get Started with ASP.NET Web API 2 (C#)

Creating a Web API Project

Visual Studio now includes Web API template which creates a new Web API project with all its dependencies automatically.

Just open Visual Studio click on File menu > New Project. This will open a New Project popup as below.

Expand Visual C# and select Web.

Select ASP.NET Web Application template and enter the name of a project and click OK.

This will open a new ASP.NET Project popup as shown below.

Select Web API in the above popup.

Click Ok to create a new Web API  project as shown below.

web api heducate

 

Test Web API

We can use the following third-party tools for testing Web API.

  1. Fiddler
  2. Postman

In this article, I will show you how to test using Postman

Postman is a free API debugging tool. You can install it on your Chrome browser or Mac.

Install it for Chrome from here.

After successful installation, open it and select the HTTP Method and enter the URL of your Web API as shown below.

Click on the Send button to send an HTTP request to the provided URL. The response is displayed below.

 

Please if you have any questions? or if you need any more details, comment below or submit your question on the Questions page.

 

Resources: some images are taken from http://www.tutorialsteacher.com

Leave a Comment

Your email address will not be published. Required fields are marked *