Creating a Simple Web API application

In this article we are going to focus on how to create a simple Web API application and access it using any browser such as Google Chrome or Internet Explorer. Web API supports GET, PUT, POST, DELETE methods and it can be used in any kind of applications such as Web Applications, Mobile Applications, Console Applications.

Here we are going to focus on how to create a simple Web API application and access it using browser as the client

Step 1: Launch Visual Studio 2013 -> File -> New Project -> Select Visual C# under Templates -> Web -> ASP.NET Web Application. Provide the name as "EmployeesAppDemo".
Step 2: In the New ASP.NET Project window -> Under Select a Template -> Select "Empty". Under Add folders and core references for -> check "Web API". Make sure "Host in the cloud" option is unchecked because we are not hosting our application in cloud -> click OK
Step 3: Go to Solution Explorer -> Right click Models folder -> Add "Employee.cs" class file -> Add below code in the Employee class.

public class Employee
public int EmpId { get; set; }
public string EmpName { get; set; }
public decimal Salary { get; set; }
public bool IsActive { get; set; }

Step 4: Right Click Controllers folder -> Add ->Controller.
Step 5: Select Web API 2 Controller - Empty -> click on Add.
Step 6: Name the controller as EmployeesController.
Step 7: Add the following code to the EmployeesController class.

using EmployeesAppDemo.Models;
using System;
using System.Collections.Generic;
using System.Web.Http;
using System.Linq;

namespace EmployeesAppDemo.Controllers
public class EmployeesController : ApiController
// employees are stored in an array
Employee[] emps = new Employee[]
new Employee(){ EmpId=1, EmpName="Priya", Salary=20000, IsActive=true},
new Employee(){ EmpId=2, EmpName="Sakshi", Salary=15000, IsActive=false},
new Employee(){ EmpId=3, EmpName="Tina", Salary=40000, IsActive=true}


/// Returns the list of employees as IEnumerable

/// All the Employees
public IEnumerable GetAllEmployees()
return emps;

/// Gets and returns the employee with the EmpId matching the parameter id value.

/// EmpId
/// Employee mathching the EmpId
public IHttpActionResult GetEmployee(int id)
var emp = emps.FirstOrDefault(e => e.EmpId == id);
return NotFound();
return Ok(emp);

Step 8: Right click project in the solution explorer -> Properties -> Select "Web" on the Left hand side -> Copy the Project Url as shown below.
project url
Step 9: Append the above copied url with api/Employees and paste the complete url in the Browser window. The complete url would look something like below:


Here the port number might vary based on the settings.

Step 10: This would prompt you to either open or save the Employees.json file.
When you open the file, you would find all the details of the Employees as shown below:


Step 11: you can also append api/Employees/1 to retrieve the specific employee details.
Url: "http://localhost:2128/api/Employees/1"
Output: {"EmpId":1,"EmpName":"Priya","Salary":20000.0,"IsActive":true}


No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: