Nowadays there are broad range of clients like browsers, mobiles, iPhone and tablets using HTTP services. These HTTP services can be built by a ASP.NET Web API framework…It contains MVC features like routing, controllers, action, results, filter but it is not an MVC Framework. It is a part of the core ASP.NET platform and can be used with MVC and other types of Web applications like ASP.NET Web Forms. It can also be used as a stand-alone Web services application.
Introduction
In this article we will see the walk through of how to create web API that returns list of students. The frontend web page uses jQuery to display the results...
Objective
The objective of this article is to explain how to work with ASP.NET web API
Let’s start by creating a sample Application through step by step procedure
- Step 1: Create a New Project Open Visual Studio 2013 -> click on File -> New Project -> Create new ASP.NET WebApplication -> Name it as StudentsApp and click OK
- Step 2: Select Empty Template and check web API as shown and click ok
Note: In the above image we can watch Web API Template which uses MVC to provide help pages. Here empty template is used in order to show web API without MVC
- Step 3: Install ASP.NET Web API package as shown
Right click on References click on Manage Nuget package and install
- Step 3: Now let’s try to add a model which represents a Student
Right click on Models Folder Click on Add a class named Student
Add the Properties to the Student Model
namespace StudentsApp.Models
{
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public string Standard { get; set; }
public decimal Marks { get; set; }
}
}
- Step 4 Now let’s add a Controller that handles HTTP requests.

In the Add Scaffold dialog, select Web API Controller - Empty. Click Add and name it as StudentsController
The scaffolding creates a file named StudentsController.cs in the Controllers folder as shown.
Add the below code to the controller
The GetAllStudents
method returns the entire list of students as an IEnumerable<Student> type.
The GetStudent
method looks up a single product by its ID.
using StudentsApp.Models;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
namespace StudentsApp.Controllers
{
public class StudentsController : ApiController
{
Student[] Students = new Student[]
{
new Student { Id = 1, Name = "Sagar", Standard = "seventh", Marks = 60 },
new Student { Id = 2, Name = "Sachin", Standard = "seventh", Marks = 70 },
new Student { Id = 3, Name = "Dravid", Standard = "seventh", Marks = 90 },
new Student { Id = 4, Name = "Ramesh", Standard = "seventh", Marks = 90 },
new Student { Id = 5, Name = "Ganguly", Standard = "seventh", Marks = 90 },
new Student { Id = 6, Name = "Rahul", Standard = "seventh", Marks = 90 },
};
public IEnumerable<Student> GetAllStudents()
{
return Students;
}
public IHttpActionResult GetStudent(int id)
{
var student = Students.FirstOrDefault((p) => p.Id == id);
if (student == null)
{
return NotFound();
}
return Ok(student);
}
}
}
Conclusion
In this article we have learned how to use Web API..In the coming articles we will see how to call this Web API with Javascript and jQuery
Reference
http://msdn.microsoft.com/en-us/library/hh833994(v=vs.108).aspx