C# 模型绑定生成一个空列表
当我试图通过邮递员发送post请求时,生成的C# 模型绑定生成一个空列表,c#,.net-core,model,C#,.net Core,Model,当我试图通过邮递员发送post请求时,生成的TestCases列表是空的。我做错了什么 这是我作为TestCases [{“输入参数”:[{“值”:“你好世界”,“类型”:“字符串”}],“预期输出”:“test123123”},{“输入参数”:[{“值”:“你好世界2”,“类型”:“字符串”}],“预期输出”:“test123123”}] 但是创建的结果模型有一个空的测试用例列表 如何复制: 邮递员要求: { "info": { "_postman_id":
TestCases
列表是空的。我做错了什么
这是我作为TestCases
[{“输入参数”:[{“值”:“你好世界”,“类型”:“字符串”}],“预期输出”:“test123123”},{“输入参数”:[{“值”:“你好世界2”,“类型”:“字符串”}],“预期输出”:“test123123”}]
但是创建的结果模型有一个空的测试用例列表
如何复制:
邮递员要求:
{
"info": {
"_postman_id": "6f252c2d-adb0-487e-ba68-d32172ebae91",
"name": "test",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
{
"name": "Create Assignment",
"request": {
"method": "POST",
"header": [
{
"key": "",
"value": "application/json",
"type": "text",
"disabled": true
},
{
"key": "Content-Type",
"name": "Content-Type",
"value": "application/json",
"type": "text"
}
],
"body": {
"mode": "urlencoded",
"urlencoded": [
{
"key": "description",
"value": "testDesc2",
"type": "text"
},
{
"key": "test_cases",
"value": "[{\"input_params\":[{\"value\":\"Hello World\",\"type\":\"string\"}],\"expected_output\":\"test123123\"}, {\"input_params\":[{\"value\":\"Hello World\",\"type\":\"string\"}],\"expected_output\":\"test123123\"}]",
"type": "text"
},
{
"key": "TestCases",
"value": "TestCases[]=1050&TestCases[]=2000\n",
"type": "text",
"disabled": true
}
]
},
"url": {
"raw": "{{localhost}}/api/assignment/create",
"host": [
"{{localhost}}"
],
"path": [
"api",
"assignment",
"create"
]
}
},
"response": []
}
],
"protocolProfileBehavior": {}
}
控制器方法:
[HttpPost("create")]
public async Task<ActionResult<Assignment>> CreateAssignment([FromForm] Assignment assignment)
{
Debug.Write(assignment);
return Ok();
}
这就是你要通过的吗?您需要为其提供Assignemnt Assignment的序列化实例-仅发送TestCases值将不起作用。@fredrik您的意思是这样的(来自dotnet doc的示例):
selectedCourses[]=1050&selectedCourses[]=2000
?我不能发送一个JSON数组吗?我不知道那是什么类型的语法。如果调用需要赋值的函数,那么JSON必须表示该类,而不仅仅是其中的一个属性。否则它怎么知道如何反序列化?@fredrik它可以反序列化通过表单数据传递的属性。如您所见,Description
映射得很好。唯一的问题是列表。当您以这种方式通过表单参数发送内容时,其中的json代码不会被解析。您需要将整个请求重新制作为请求主体中的json数据—无表单参数。现在它是空的,因为字符串不是测试用例列表。
public class Assignment : EntityBase
{
public Assignment()
{
this.CreatedDate = DateTime.UtcNow;
}
[Required]
public string Description { get; set; }
[JsonProperty("test_cases")]
[BindProperty(Name = "test_cases")]
[Required]
public virtual List<TestCase> TestCases { get; set; }
public virtual List<Solution> Solutions { get; set; }
[DataType(DataType.Date)]
[JsonProperty("created_date")]
[Required]
public DateTime CreatedDate { get; private set; }
}
public class TestCase : EntityBase
{
[JsonProperty("input_params")]
[BindProperty(Name = "input_params")]
public virtual List<InputParam> InputParams { get; set; }
[JsonProperty("expected_output")]
[BindProperty(Name = "expected_output")]
public string ExpectedOuput { get; set; }
[JsonIgnore]
public int AssignmentId { get; private set; }
[JsonIgnore]
public virtual Assignment Assignment { get; private set; }
}
public abstract class EntityBase
{
[Required]
public int Id { get; private set; }
}