POST
– Create new resourceGET
– Retrieve resourcePUT
– Update resourceDELETE
– Delete resourceStudent
class with public properties
public class Student { public int Id { get; set; } public string Name { get; set; } public float Gpa { get; set; } }
public class StudentRepository { // List of students private List<Student> students = new List<Student>(new Student[] { new Student { Id = 1, Name = "Bob Smith", Gpa = 2.5f }, new Student { Id = 2, Name = "Sue White", Gpa = 3.0f } }); public List<Student> GetAllStudents() { return students; } public Student GetStudent(int id) { // Return back garbage student if the ID was not found var student = students.FirstOrDefault(p => p.Id == id); if (student == null) return new Student { Id = -1, Name = "NULL" }; return student; } public bool AddStudent(Student student) { if (student == null) throw new ArgumentNullException("student"); // Make sure student ID is unique var findStudent = students.FirstOrDefault(p => p.Id == student.Id); if (findStudent == null) { students.Add(student); return true; } return false; } public bool UpdateStudent(Student student) { // See if a student with this ID exists so he can be replaced int i = students.FindIndex(p => p.Id == student.Id); if (i == -1) return false; students[i] = student; return true; } public bool DeleteStudent(int stuId) { // See if a student with this ID exists so he can be deleted int i = students.FindIndex(p => p.Id == stuId); if (i == -1) return false; students.RemoveAt(i); return true; } }
Get
,
Post
, Update
, and Delete
using StudentWebService.Models;
public class StudentController : ApiController { private static StudentRepository studentRepo = new StudentRepository(); // GET api/student public IEnumerable<Student> GetAll() { return studentRepo.GetAllStudents(); } // GET api/student/2 public Student Get(int id) { var student = studentRepo.GetStudent(id); if (student == null) throw new HttpResponseException(HttpStatusCode.NotFound); else return student; } // POST api/student public void Post(Student student) { if (!studentRepo.AddStudent(student)) throw new HttpResponseException(HttpStatusCode.NotFound); } // PUT api/student public void Put(Student student) { if (!studentRepo.UpdateStudent(student)) throw new HttpResponseException(HttpStatusCode.NotFound); } // DELETE api/student/2 public void Delete(int id) { if (!studentRepo.DeleteStudent(id)) throw new HttpResponseException(HttpStatusCode.NotFound); } }
http://localhost:port/api/studentOn IE returns JSON:
[[{"Id":1,"Name":"Bob Smith","Gpa":2.5},{"Id":2,"Name":"Sue White","Gpa":3.0}]In Chrome returns XML:
<ArrayOfStudent xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/StudentWebService.Models"> <Student> <Gpa>2.5</Gpa> <Id>1</Id> <Name>Bob Smith</Name> </Student> <Student> <Gpa>3</Gpa> <Id>2</Id> <Name>Sue White</Name> </Student> </ArrayOfStudent>
http://localhost:port/api/student/2returns
[{"Id":2,"Name":"Sue White","Gpa":3.0}]
Accept
header requests application/xml
with
a higher weight (q=0.9) than application/json
(*/* with q=0.8)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept
header which gives XML and JSON
equal weights
Accept: text/html, application/xhtml+xml, */*