This article will demonstrate how to use ASP.NET MVC in C# to create a basic RESTful API. When a specific student ID is searched, this API will return student data (including name, date of birth, zip code, major department, and student ID). This beginner-friendly guide offers step-by-step instructions, code walkthroughs, and explanations to help you comprehend and complete the project.

Tools Required
- Visual Studio 2019/2022 (Community Edition is free)
- .NET Framework (4.7 or later)
- Basic knowledge of C# syntax
Step 1. Create a New Web API Project
- Open Visual Studio.
- Click on File > New > Project.
- Choose ASP.NET Web Application (.NET Framework).
- Name your project StudentAPI and click OK.
- Select the Web API template and click Create.
This sets up a basic project with preconfigured folders for Models, Controllers, and API routes.
Step 2. Create the Student Model
This model defines the structure of the student data.
File: Models/Student.cs
using System;
namespace StudentAPI.Models
{
public class Student
{
public string StudentId { get; set; } // Alphanumeric ID e.g. S1001
public string Name { get; set; }
public DateTime DateOfBirth { get; set; } // e.g. 2001-06-15
public string ZipCode { get; set; } // e.g. 600001
public string Major { get; set; } // e.g. Computer Science
}
}
Explanation
- StudentId: Alphanumeric identifier for the student.
- Name: Full name of the student.
- DateOfBirth: The date of birth, stored as a DateTime object.
- ZipCode: Postal code of the student’s residence.
- Major: The department or field of study.
Step 3. Create the API Controller
The controller processes incoming HTTP requests and returns responses.
File: Controllers/StudentController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using StudentAPI.Models;
namespace StudentAPI.Controllers
{
public class StudentController : ApiController
{
private static List<Student> students = new List<Student>
{
new Student
{
StudentId = "S1001",
Name = "Carlos",
DateOfBirth = new DateTime(2000, 3, 12),
ZipCode = "600045",
Major = "Mechanical Engineering"
},
new Student
{
StudentId = "S1002",
Name = "Peter",
DateOfBirth = new DateTime(2001, 7, 25),
ZipCode = "641001",
Major = "Electronics and Communication"
},
new Student
{
StudentId = "S1003",
Name = "Scott",
DateOfBirth = new DateTime(1999, 12, 30),
ZipCode = "500081",
Major = "Computer Science"
}
};
// GET api/student/{id}
public IHttpActionResult GetStudentById(string id)
{
var student = students.FirstOrDefault(
s => s.StudentId.Equals(id, StringComparison.OrdinalIgnoreCase));
if (student == null)
{
return NotFound();
}
return Ok(student);
}
}
}
Explanation
- We use an in-memory List<Student> to simulate a database.
- The method GetStudentById accepts an ID as input.
- It searches for the student in the list using LINQ.
- If found, it returns the student data in JSON format.
- If not found, it returns a 404 (Not Found) HTTP response.
Step 4. Configure Web API Routing
Ensure your app correctly maps incoming API requests.
File: App_Start/WebApiConfig.cs
using System.Web.Http;
namespace StudentAPI
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Enable attribute routing
config.MapHttpAttributeRoutes();
// Define default route
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
}
Explanation
- Enables attribute routing.
- Sets a default route pattern: api/{controller}/{id}.
- This means a call to api/student/S1001 will map to StudentController.
Step 5. Run and Test Your API
- Press F5 or click Start Debugging in Visual Studio.
- The browser will open. Change the URL to:
http://localhost:1234/api/student/S1002
(Replace the port 1234 with your actual port number.)
Example Output
{
"StudentId": "S1002",
"Name": "peter",
"DateOfBirth": "2000-08-25 T00:00:00",
"ZipCode": "641001",
"Major": "Electronics and Communication"
}
Happy Learning!