Basics of Web API


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.

    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 Project  and choose Empty option and  Web API.

      Default structure will be like this.

    Add a model class named with User.cs in models folder.
Add following property in user class.
Add a new controller named with UsersController.
Select Controller folder and right click , Go to add-> controller.
Choose “Web API controller with action using Entity Framework”.
Select model class and give any name to controller.
Click on Add.

    Controller will be created with several action.
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.

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.

We will get response as following.

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(“*“, “*“, “*“);

By doing this cors will be enable.



One thought on “Basics of Web API

  • August 21, 2017 at 4:21 am

    Nice Article on Web API.


Leave a Reply

Your email address will not be published. Required fields are marked *