Thursday, 1 September 2016

Getting started with ASP.Net Web API

Introduction to Web API

ASP.Net Web API is a framework for building, consuming HTTP based service. Advantage of Web API is that it can be consumed over web browse 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 web browser. But now a days we have various client like web browser, mobile application and other handheld device applications. These devices contains many apps for different purposes.

Suppose you have create a service and you like to expose the service to web browser and all latest apps in handheld devices. Here you can create multiple services for each clients which does not make any sense in this era. Here Web API comes into the picture.

Below image explains a Web API in 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 slow connection like 2G mobile internet.
No configuration needed like WCF services.
Supports clean URL



No comments:

Post a Comment