How To Create ASP.NET Web API REST Service

Today's application need to be communicated on all cross platforms to fulfill the need of today's modern application, to fulfill these requirement lots of technologies are invented and they are used as per requirement. The Web API is one of the latest technology to fulfill the requirement of enterprise application.
Let's learn about it step by step:
What is ASP.NET Web API
The ASP.NET Web API is one of the Microsoft open source technology to build the powerful REST Services which will communicate across all platform and devices over HTTP.
Let us implement it practically by creating one sample application.

Step 1 :
Create ASP.NET Web API Service
  1. "Start", then "All Programs" and select "Microsoft Visual Studio 2015".
  2. "File", then "New" and click "Project", then select "ASP.NET Web Application Template" and provide the Project a name as you wish and click on OK, then the following windows will appear; select empty template and check on Web API as in the following image.

  3. Choose MVC empty application option and click OK.
  4. Now after adding the MVC project, the Solution Explorer will look like the following:

After creating the ASP.NET Web API project, the preceding folder structure and files are added into the project by default which are same as MVC project, so let us learn about them in brief as in the following:

  • App_Start folder
    This folder contains the application configuration details such as routing, authentication, filtering of URL and so on.
  • Controller
    This folder contains the controller and their methods. The controller is responsible for processing the user request and return output as a view.
  • Models
    This folder contains the entities or properties used to store the input values.
Step 2 : Create Model Class.

Right click on Model folder in the created Web API application, give the class name EmpModel or as you wish and click on OK.

Now write the following code into the EmpModel Class.
public class EmpModel  
        public string Name { get; set; }  
        public string City { get; set; }  

Step 3 : Add Web API Controller.

Right click on Controller folder in the created Web API application and Web API 2 Empty controller as in the following,

Now click on add and give the class name Home or as you wish and click OK as:

Now we have added Model class and web API controller class. Let's create the method into the HomeController class as in the following,

using System;  
using System.Web.Http;  
namespace CreatingWebAPIService.Controllers  
    public class HomeController : ApiController  
        public bool AddEmpDetails()  
            return true;  
            //write insert logic  
        public string GetEmpDetails()  
            return "Vithal Wadje";  
        public string DeleteEmpDetails(string id)  
            return "Employee details deleted having Id " + id;  
        public string UpdateEmpDetails(string Name, String Id)  
            return "Employee details Updated with Name " + Name + " and Id " + Id;  
In this above code we have created four methods for Insert, Update, Display and delete the records .The above web API controller methods defined with HTTP methods which are the following,
  • GET: Get the resource (Records) from particular source such as SQL database.
  • POST: Used to insert the records into the particular source such as SQL, Oracle database.
  • PUT: Used to modify the resource or records.
  • DELETE : Used to delete the specific resource or record from particular source.
 Step 4: Run the application.
Now everything is ready lets run the application, it will throw the following error:


The reason of above error is we are not following the url structure which are set into the routing of web API application. The following is the default URL structure of ASP.NET Web API defined into the WebApiConfig.cs file.

From the above routing structure, we can access the resource using  url format i.e Base url+api+apicontroller. Unlike MVC controller the web controller by default access all the methods just using controller name. Now our service is ready. Let's test it using REST client browser extension because browser only supports Get request directly from URL not others .

Our REST Service URL will be http://localhost:56290/api/Home which is described as follows,
  • http://localhost:56290: Is the base address of web API service, It can be different as per your server.
  • api: It is the used to differentiate between Web API controller and MVC controller request .
  • Home: This is the Web API controller name.
Step 5: Testing Web API REST service using REST client.

 Now let us test the application using REST client as:


In the above image Web API REST Service, HTTP POST method returns the 200 Status code means REST service is successfully executed.
GET Method

In the above image Web API REST Service, HTTP GET method returns the 200 Status code means REST service is successfully executed and return the XML format output.

  • The  Web API REST service by default return the output as per browser default message header such as XML or JSON.
The above XML output return in Firefox browser., Now let's change the browser as IE, then output will return in JSON format as in the following,

From the above output its clear that output format by default depend on browser message format header.
Delete Method

In the above image Web API REST Service, HTTP Delete method returns the 200 Status code means REST service is successfully executed and return the XML format output. I hope from the preceding examples we learned how to create Web API REST Service.

  • Download the Zip file of the sample application for a better understanding.
  • In my next article, Introduction ASP.NET Web API we will learn what is Web API REST Service, When and where to use Web API REST Service.

I hope this article is useful for all readers. If you have any suggestions, then please mention it in the comment section.

Post a Comment

Protected by Copyscape
Copyright © Compilemode