“JSON 值无法转换为 System.Collections.Generic.IEnumerable`1[StudentsManagementSystem.Entity.CreateStudentEntity]

"The JSON value could not be converted to System.Collections.Generic.IEnumerable`1[StudentsManagementSystem.Entity.CreateStudentEntity]

我是 .NET 和 MVC 架构的新手。我正在做一个小型项目,为学生信息处理创建 API。但是,我在尝试使用 curl 创建新学生集合时遇到以下错误:

{"type":"https://tools.ietf.org/html/rfc7231#section-6.5.1","title":"One or more validation errors occurred.","status":400,"traceId":[REDACTED],"errors":{"$":["The JSON value could not be converted to System.Collections.Generic.IEnumerable`1[StudentsManagementSystem.Entity.CreateStudentEntity]. Path: $ | LineNumber: 0 | BytePositionInLine: 1."]}}

这是应该处理请求的控制器 class:

namespace StudentsManagementSystem.StudentController{
[ApiController]
[Route("api/student/Collections")]
public class StudentCollectionController: ControllerBase
{
    private IStudentRepository _students;
    private IMapper _mapper;
    public StudentCollectionController(IMapper mapper, IStudentRepository student)
    {
        _mapper = mapper;
        _students = student;
    }

    [HttpPost]
    public ActionResult<IEnumerable<Student>> CreateStudentCollection(IEnumerable<CreateStudentEntity> newStudents)
    {
        Console.WriteLine("DEBUGG");
        if (newStudents.Count<CreateStudentEntity>()< 1) return BadRequest();
        var map = _mapper.Map<IEnumerable<Student>>(newStudents);
        foreach(var student in map)
        {
            _students.AddStudent(student);
        }
        return Ok();
    }
}

CreateStudentEntity Class:

namespace StudentsManagementSystem.Entity{
public class CreateStudentEntity
{
    public string StudentName { get; set; }

    public string MatricNo { get; set; }
    public string Level { get; set; }
    public string Dept { get; set; }
    public DateTime DoB { get; set; }
    public int YOA { get; set; }
    public ICollection<Course> Results { get; set; } = new List<Course>();
}

Student 实体 class:

namespace StudentsManagementSystem.Entity{
public class Student
{
    public string StudentName { get; set; }
    public int StudentId { get; set; }
    public string MatricNo { get; set; }
    public string Level { get; set; }
    public string Dept { get; set; }
    public DateTime DoB { get; set; }
    public int YOA { get; set; }
    public ICollection<Course> Results { get; set; } = new List<Course>();


}

curl请求:

curl -X POST http://localhost:5000/api/student/collections -H "Content-Type:application/json" -d "{{}}"

注意:DEBUGG没有写在控制台上。

请帮助n00b :(

显然您还没有在 AutoMapper 设置中创建映射以将“CreateStudentEntity”类型的对象转换为“Student”类型的对象。

勾选AutoMapper Documentation

在你的情况下它会是这样的:

public class StudentProfile : Profile
{
     public StudentProfile()
     {
        CreateMap<Student, CreateStudentEntity>()
         .ReverseMap();
     }
}

这是您需要的最少代码。

您在 cURL 请求中发送的值 - {{}} - 无效 JSON 且不代表数组。 JSON 中的数组由方括号 [] 表示,对象由大括号 {}.

表示

因此,如果您想将空数组传递给控制器​​,您可以使用 []

如果你想传递一个包含空对象的数组,它将是[{}],例如:

curl -X POST http://localhost:5000/api/student/collections -H "Content-Type:application/json" -d "[{}]"

要在数组中传递 CreateStudentEntity 个对象,您需要包含属性,例如:

[{"MatricNo": "ABC123", "StudentId": 123, ...}, {object2...}, ...]