Step 1 : Create Empty Web API application using Visual Studio 13/15/17/19
Step2: In Solutions Explorer > Model> Right Click> Add > Class
Students.cs
public class Students
{
public int SId{ get; set; }
public string SName { get; set; }
public string Email { get; set; }
public string Course { get; set; }
}
Step3: In Solutions Explorer > Controller > Right Click> Add > Controller
StudentController
IList Students = new List()
{
new Students()
{
SId = 1, SName = "Smith", Email = "sm@Delhi.com", Course = "Networking"
},
new Students()
{
SId = 2, SName = "Jone Doe", Email = "jd@London.co.uk", Course = "Cyber"
},
new Students()
{
SId = 3, SName = "Kila Mek", Email = "km@white.com", Course = "IT"
},
new Students()
{
SId = 4, SName = "Ros Lin", Email = "Ros@Goa.in", Course = "Hacking"
},
new Students()
{
SId = 5, SName = "S Chandra", Email = "som@banglore.in", Course = "Software"
},
};
//get api/student
public IList GetAllStudents()
{
//Return list of all employees
return Students;
}
//get api/student/5
public Students GetStudentDetails(int id)
{
//Return a single employee detail
var Student = Students.FirstOrDefault(e => e.SId == id);
if (Student == null)
{
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
}
return Student;
}
Step4: Build and Debug.
Api Successful Created.....
Now open cmd - Command Prompt
Step2: In Solutions Explorer > Model> Right Click> Add > Class
Students.cs
public class Students
{
public int SId{ get; set; }
public string SName { get; set; }
public string Email { get; set; }
public string Course { get; set; }
}
Step3: In Solutions Explorer > Controller > Right Click> Add > Controller
StudentController
IList
{
new Students()
{
SId = 1, SName = "Smith", Email = "sm@Delhi.com", Course = "Networking"
},
new Students()
{
SId = 2, SName = "Jone Doe", Email = "jd@London.co.uk", Course = "Cyber"
},
new Students()
{
SId = 3, SName = "Kila Mek", Email = "km@white.com", Course = "IT"
},
new Students()
{
SId = 4, SName = "Ros Lin", Email = "Ros@Goa.in", Course = "Hacking"
},
new Students()
{
SId = 5, SName = "S Chandra", Email = "som@banglore.in", Course = "Software"
},
};
//get api/student
public IList
{
//Return list of all employees
return Students;
}
//get api/student/5
public Students GetStudentDetails(int id)
{
//Return a single employee detail
var Student = Students.FirstOrDefault(e => e.SId == id);
if (Student == null)
{
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
}
return Student;
}
Step4: Build and Debug.
Api Successful Created.....
Now open cmd - Command Prompt