WebAPI在开始时添加类标题

时间:2019-05-15 13:04:05

标签: c# json asp.net-mvc rest json.net

我通过ASP.NET Web API将REST API响应序列化为JSON。 这是我的结果:JSON Format

我正在通过以下链接访问此结果:http://192.168.2.35:55281/api/Students。因此,我想使我的JSON格式以类标题开头,例如:

"Students": [
  {
    "Id": 18,
    "Type": "Sayısal",
    "Class_Id": 9
  }
]

但是不幸的是,正如您从顶部的链接中看到的那样,我没有Students的标题。如何在Web API上激活它?

此处是我的控制器:

public class StudentsController : ApiController
{
    private DBClassroomEntities db = new DBClassroomEntities();

    // GET: api/Students
    public IQueryable<Student> GetStudent()
    {
        return db.Student;
    }

    // GET: api/Students/5
    [ResponseType(typeof(Student))]
    public IHttpActionResult GetStudent(int id)
    {
        Student student = db.Student.FirstOrDefault(s=>s.Id==id);

        if (student == null)
        {
            return NotFound();
        }

        return Ok(student);
    }

    // PUT: api/Students/5
    [ResponseType(typeof(void))]
    public IHttpActionResult PutStudent(int id, Student student)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        if (id != student.Id)
        {
            return BadRequest();
        }

        db.Entry(student).State = EntityState.Modified;

        try
        {
            db.SaveChanges();
        }
        catch (DbUpdateConcurrencyException)
        {
            if (!StudentExists(id))
            {
                return NotFound();
            }
            else
            {
                throw;
            }
        }

        return StatusCode(HttpStatusCode.NoContent);
    }

    // POST: api/Students
    [ResponseType(typeof(Student))]
    public IHttpActionResult PostStudent(Student student)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        db.Student.Add(student);
        db.SaveChanges();

        return CreatedAtRoute("DefaultApi", new { id = student.Id }, student);
    }

    // DELETE: api/Students/5
    [ResponseType(typeof(Student))]
    public IHttpActionResult DeleteStudent(int id)
    {
        Student student = db.Student.Find(id);
        if (student == null)
        {
            return NotFound();
        }

        db.Student.Remove(student);
        db.SaveChanges();

        return Ok(student);
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            db.Dispose();
        }
        base.Dispose(disposing);
    }

    private bool StudentExists(int id)
    {
        return db.Student.Count(e => e.Id == id) > 0;
    }
}

1 个答案:

答案 0 :(得分:1)

  1. 创建一个新的类StudentList

    public class StudentList
    {
        public List<Student> Students { get; set; }
    }
    
  2. 更改此方法:

    // GET: api/Students
    public IQueryable<Student> GetStudent()
    {
        return db.Student;
    }
    

    对此:

    // GET: api/Students
    [ResponseType(typeof(StudentList))]
    public IHttpActionResult GetStudents()
    {
        StudentList studentList = new StudentList() 
        {
            Students = db.Student.ToList();
        }
    
        return Ok(studentList);
    }
    

此方法返回的JSON类似于此:

{
  "Students": [
    {
        "Id": 18,
        "Type": "Sayısal",
        "Class_Id": 9,
        "Group_Id": 1,
        "FName": "Hüseyin",
        "LName": "Biçen"
    },
    {
        "Id": 20,
        "Type": "Yabancı Dil",
        "Class_Id": 7,
        "Group_Id": 6,
        "FName": "Hasan",
        "LName": "Naser"
    }
  ]
}