Basics of Web API

api_img
Today, Mostly people are using mobile devices in place of web application such as iPhone, mobile phones and tablets in their daily lives. These devices also have many apps to make their life easier like Gmail, Twitter, Facebook etc. In other word we can say web based application is not enough to reach its client.
So, if you like to expose your service data to the browsers and as well as all these modern devices apps in fast and simple way, you should have an API which is compatible with browsers and all these devices.
Web API is the best solution for this. It is a framework for exposing your data and service to different devices.
Web API is an open source platform for building Restful services over the .Net framework. It runs over HTTP.  HTTP is stateless protocol, which means the connection between client and server is lost once the transaction ends.
WHY WEB API?
    If we need a web service and do not need SOAP then web api is best option.
    It is an open source.
    It runs over HTTP and easy to define, expose and consume in a Restful way.
    We don’t need to define any extra config settings for different devices unlike WCF Rest service,
    It is light weight architecture and good for devices which have limited bandwidth like smart phones.
How to create WEB API?
    Open Visual studio.
    Create a New project from select menu File > New > Project > Web> New Asp.net Project  and choose Empty option and  Web API.
1.1
      Default structure will be like this.
1.2
    Add a model class named with User.cs in models folder.
1.3
Add following property in user class.
1.4
Add a new controller named with UsersController.
Select Controller folder and right click , Go to add-> controller.
1.5
Choose “Web API controller with action using Entity Framework”.
1.6
Select model class and give any name to controller.
1.7
Click on Add.
1.8
    Controller will be created with several action.
1.9
Add following code in GetUsers() method.
List<User> users = new List<Models.User>();
users.Add(new Models.User() {ID=1,Name=”ABC” , Address=”New Delhi” ,Phone=”123456789″ });
users.Add(new Models.User() {ID=2,Name=”XYZ” , Address=”Mumbai” ,Phone=”2345678909″ });
users.Add(new Models.User() { ID = 3, Name = “PQR”, Address = “Bangalore”, Phone = “09876555443” });
users.Add(new Models.User() { ID = 4, Name = “TUV”, Address = “Chennai”, Phone = “233422323” });
return users.AsQueryable();
    Now Web Api is created successfully. But we need to add some configuration setup in web.config file. Open web config file and add following code in <system.webServer>  tag.
<directoryBrowse enabled=”true”/>
    Now run the application once.
2.0

Test Web API:

To consume web service install a developer tool for interacting with web services like poster(Firefox) and type following url.
http://localhost:51103/api/Users and click on Get.
2.1
We will get response as following.
2.2
Our  web api is ready to response.
Every time when we are creating any WEB Services or API’s we should enable CORS.
What is CORS?
ASP.NET Web API – CORS Support in ASP.NET Web API 2. Cross-origin resource sharing (CORS) is a World Wide Web Consortium (W3C) specification (commonly considered part of HTML5) that lets JavaScript overcome the same-origin policy security restriction imposed by browsers.
By enabling cors in our Web api we can access it from anywhere that are connected to same network. If we want to access Web API globally then we need to host our web service. That will be explained in my next blog.
For enabling Cors  in WEB API there are many method , we can enable by setting configuration in web config, or we can enable cors on application level(On start of application).
I always follow second step to enable cors.
For this we need to add reference of  System.Web.Http dll that is easily available on google.
After that we need to add following code in App_start> WebApiConfig.cs file.
//Enabling CORS
var cors = new EnableCorsAttribute(“*“, “*“, “*“);
config.EnableCors(cors);
By doing this cors will be enable.
THANKS
References:
https://docs.microsoft.com/en-us/dotnet/framework/wcf/wcf-and-aspnet-web-api
https://docs.microsoft.com/en-us/aspnet/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api
https://www.tutorialspoint.com/asp.net_mvc/asp.net_mvc_web_api.htm
https://docs.microsoft.com/en-us/aspnet/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api