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

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 a wide range of clients like a 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:

You can use ASP.Net web API with MVC and ASP.Net web forms as well.
ASP.Net Web API doesn’t stick to any architectural style. A RESTful service can be built on top of ASP.Net web API.

Now you might have a question in your mind.
We have WCF already running in the market then why ASP.Net Web API?
We can also develop HTTP based services and can develop RESTful services using WCF.

There could be multiple reasons to use WCF over ASP.Net Web API

1. If you have SOAP-based service then go to WCF service.
2. If you are limited to .NET 3.5 version then go for WCF service.

One major advantage of ASP.Net Web API over WCF services is that to implement WCF services we must do lots of configuration to complete the task whereas in ASP.Net Web API it requires zero configuration to do the same task.

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 a 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 Lightweight and good for a slow connection to the 2G mobile internet.
No configuration needed like WCF services.
Supports clean URL

ASP.Net Web API Routing

ASP.Net Web API routing is similar to MVC routing.
So what is Web API routing?
Web API routing is the way to route the incoming request

Assembly used in ASP.Net Web API

System.net.Http
System.Web.Http
System.Web.Http.WebHost

Read this - What is Microservice Architecture?

Summary –

  • A framework to create HTTP based services
  • It supports HTTP verbs – GET, POST, PUT DELETE
  • ASP.Net Web API can be consumed by a 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
 
Keep following SharePointCafe.Net

Leave a Comment

RSS
YouTube
YouTube
Instagram