Getting started with ASP.Net Web API

Introduction to Web API

ASP.Net Web API is a framework for building, consuming HTTP based service. The advantage of Web API is that it can be consumed over a web browser and mobile application as well.

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 
  • DELETE

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

Comments

Popular