WebAPI Basics

Overview

 
Applications run in several type of client devices such as desktop, tablets and mobile devices. Below we are going to see how the application works without WebAPI and how we can make the application more feasible using web API.
 

Application without Web API

  1. Client devices send the request to the server where the application is returned without webAPI.
  2. Server processes the request which comes from the client devices.
  3. Server sends the response back.
When client device is desktop, it sends the request in XML format. The server in which the application is returned without webAPI processes the request and sends back the response in XML format.
 
But, when the client devices are tablets and smartphones, they are incapable of sending the request in XML format. So this devices sends the request in JSON format. Now the server which is only capable of handling the request in the JSON format cannot process this request. The immediate solution is to create an application also without WebAPI to handle the JSON request and send back the response in JSON format. Going by this logic, that means for every request type there has to be separate application which is not feasible.
 
So we need an application that can give a response in any format based on request type.
 

Application in the server with webAPI

 
Now, when the client sends the request in either XML or JSON format, the server processes that request and sends back the response either in XML or JSON format, whichever is required.
 
JSON vs XML
  • JSON is lightweight as compared to XML. 
  • In case of XML, If the maximum message size quota for incoming messages (65536) has been exceeded then we need to increase the quota. For doing so, use the MaxReceivedMessageSize property on the appropriate binding element.
ASP.Net Web API
  • It is an open source framework from .NET used for developing services.
  • It is a light weight architecture and good for devices which have limited bandwidth like smart phones.
  • It supports CRUD operations.
  • It supports ASP.NET MVC features such as routing, controllers, action results, etc. 
  • It is a light weight architecture and good for devices which have limited bandwidth like smart phones.
  • It exposes data and services to a variety of browsers and client devices like mobiles, tablets, etc.
Highlights
  • To understand the project structure of ASP.Net WebAPI application. 

Demo Steps

 
In order to create an ASP.NET Web API application for exposing product details as a service, try out the following demo steps.
 
Step 1
 
Add a new project. Choose ASP.NET Web Application template and name it as CarWebService.
 
You will get the below folder structure,
 
 
Step 2
 
Open WebApiConfig.cs file and observe the Register() method.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web.Http;  
  5.   
  6. namespace CarWebService  
  7. {  
  8.     public static class WebApiConfig  
  9.     {  
  10.         public static void Register(HttpConfiguration config)  
  11.         {  
  12.             // Web API configuration and services  
  13.   
  14.             // Web API routes  
  15.             config.MapHttpAttributeRoutes();  
  16.   
  17.             config.Routes.MapHttpRoute(  
  18.                 name: "DefaultApi",  
  19.                 routeTemplate: "api/{controller}/{id}",  
  20.                 defaults: new { id = RouteParameter.Optional }  
  21.             );  
  22.         }  
  23.     }  
  24. }  
Here, you can observe that the default routeTemplate is api/{controller}/{id}. Add {action} in the routeTemplate as highlighted in the below code to include the action name as part of the Url.
 
Step 3
 
Now, go to the controllers folder which contains two files as below,
 
 
Open Values controller.cs and see the code snippet,
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Net.Http;  
  6. using System.Web.Http;  
  7.   
  8. namespace CarWebService.Controllers  
  9. {  
  10.     public class ValuesController : ApiController  
  11.     {  
  12.         // GET api/values  
  13.         public IEnumerable<string> Get()  
  14.         {  
  15.             return new string[] { "value1""value2" };  
  16.         }  
  17.   
  18.         // GET api/values/5  
  19.         public string Get(int id)  
  20.         {  
  21.             return "value";  
  22.         }  
  23.   
  24.         // POST api/values  
  25.         public void Post([FromBody]string value)  
  26.         {  
  27.         }  
  28.   
  29.         // PUT api/values/5  
  30.         public void Put(int id, [FromBody]string value)  
  31.         {  
  32.         }  
  33.   
  34.         // DELETE api/values/5  
  35.         public void Delete(int id)  
  36.         {  
  37.         }  
  38.     }  
  39. }  
Here, the WebAPI methods for requests like Get, Post, Put, Delete has been created by default.
 

Parameter Binding

 
By default, webAPI takes the primitive parameter value from the query string and complex type parameter from the request body.
 
[FromBody]
 
We decorate the [FromBody] attribute on action method to get the value of primitive type parameter from the request body which is opposite to the default rule.
 
Note
The [FromBody] attribute can be applied on only one primitive parameter of an action method.
 
[FromUri]
 
We decorate [FromUri] attribute on action method to force webAPI to get the value of complex type from query string.
 

Summary

 
In this article, we learned what is ASP.NET WebAPI and why we need to use webAPI and parameter binding in WebAPI. Hope you like the article Until next time, Happy Reading  Cheers