What is Web API? When should we use in our projects?

Introduction to Web API

Web API is the Microsoft open source technology for developing REST services based on HTTP protocol. ASP.Net Web API is a framework for building, consuming HTTP based service. The advantage of Web API is that it can be consumed by wide range of clients like web browser and mobile applications.

ASP.Net Web API was released with ASP.Net MVC 4. Web API can be used with MVC or normal ASP.Net forms.
Few Web APIs are Google MAP, Facebook, Twitter, Amazon.

Remember HTTP != REST. It means ASP.Net Web API is not an architectural style.

ASP.Net Web API uses common HTTP verbs:
  • GET 
  • POST 
  • PUT 
Please have a look at below code:
 // GET api/values
        public IEnumerable<string> Get()
            return new string[] { "value1", "value2" };

        // GET api/values/5
        public string Get(int id)
            return "value";
        // POST api/values
        public void Post([FromBody]string value)

        // PUT api/values/5
        public void Put(int id, [FromBody]string value)

        // DELETE api/values/5
        public void Delete(int id)


Why Web API?

In earlier days we had specific clients like a web browser. But nowadays we have various clients like web browser, mobile application, and other handheld device applications. These devices contain many apps for different purposes.

Suppose you have created a service and you like to expose the service to a web browser and all latest apps in handheld devices. Here you can create multiple services for each client which do not make any sense in this era. Here Web API comes into the picture.
Below image explains a Web API in a simple way.

Introduction to ASP.Net web api

ASP.Net Web API supports JSON as well as XML data exchange format.

When to go for ASP.Net Web API?

If you have a web service and you don't need SOAP then go for Web API. 
It is Light weight and good for a slow connection like 2G mobile internet.
No configuration needed like WCF services.
Supports clean URL

Summary -
  • A framework to create HTTP based services
  • It supports HTTP verbs - GET, POST, PUT DELETE
  • ASP.Net Web API can be consumed by wide range of clients like web browsers, mobile apps and more.
  • ASP.Net uses HTTP as application protocol.
  • It supports IIS hosting and self hosting
  • By default ASP.Net Web API returns JSON data.
  • It supports ASP.Net routing
  • ASP.Net Web API implementation can be done with ASP.Net web forms, MVC, and ASP.Net Core



SharePoint Interview Questions and Answers

Calling ASP.Net WebMethod using jQuery AJAX

What is Cloud Computing - A guide for beginners

How to Crop and Upload image using JQuery