Я установил веб-контроллер API с Entity Framework. Всякий раз, когда я размещаю один json объект, это работает. Но я продолжаю получать ошибки всякий раз, когда объект JSON включен в массив [{}, {},]. Ошибка ниже через почтальона:
"Message": "The request is invalid.",
"ModelState": {
"incidence": [
"Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'TSCAPP.Models.Incidence' because the type requires a JSON object (e.g. {\"name\":\"value\"}) to deserialize correctly.\r\nTo fix this error either change the JSON to a JSON object (e.g. {\"name\":\"value\"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.\r\nPath '', line 1, position 1."
POST-контроллер:
[ResponseType(typeof(Incidence))]
public IHttpActionResult PostIncidence(Incidence incidence)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.Incidences.Add(incidence);
db.SaveChanges();
return CreatedAtRoute("DefaultApi", new { id = incidence.RegistrationID }, incidence);
}
Модель инцидента
public class Incidence
{
[Key]
public int RegistrationID { get; set; }
[Required]
public string Name { get; set; }
[Required]
[MinLength(6)]
public string TSCNO { get; set; }
[Required]
[DataType(DataType.EmailAddress)]
public string EmailAddress { get; set; }
public string CellPhone { get; set; }
public string IncidenceType { get; set; }
public string RequestType { get; set; }
public string County { get; set; }
public string ExactLocation { get; set; }
public string Request { get; set; }
public int? RoleID { get; set; }
[DataType(DataType.MultilineText)]
public string FeedBack { get; set; }
}
JSON файл отправлен
[ {"RegistrationID": 1, "Name": "Evan", "TSCNO": "662621", "EmailAddress": "evan@mail.com", "CellPhone": "254722543778", "IncidenceType": null, "RequestType" : null, "County": "West Pokot", "ExactLocation": "Kapenguria", "Request": "Nil", "RoleID": 2, "FeedBack": ""}, {"RegistrationID": 2, " Имя ":" Джон "," TSCNO ":" 607921 "," EmailAddress ":" john@mail.com "," CellPhone ":" 254700172168 "," IncidenceType ": null," RequestType ": null," County ":" Kwale "," ExactLocation ":" Mkongani "," Request ":" depenats "," RoleID ": 2," FeedBack ":" "}]