我正在使用自定义模型绑定器,以便在我的模型Binding中添加一些自定义逻辑。
这是我的DTO:
public class TaskDto
{
public int Id { get; set; }
[MaxLength(100), Required]
public string Name { get; set; }
public List<StepDto> Steps { get; set; }
public List<ResourceDTO> Resources { get; set; }
}
步骤DTO:
public class StepDto
{
public int Id { get; set; }
[MaxLength(100)]
public string Description { get; set; }
public StepType StepType { get; set; }
}
ResourceDTO:
public class ResourceDTO
{
public int Id { get; set; }
[MaxLength(250), Required]
public string Title { get; set; }
[Required]
public string Link { get; set; }
public string MetaTagUrl { get; set; }
[Required, Range(1, 1)]
public ResourceType ResourceType { get; set; }
}
其中ResourceType
是枚举(现在只有1作为值。)
我尝试使用此link创建自定义模型绑定器。
这是我的API动作方法签名:
[HttpPut]
[Route("datacapture/{activityId}/tasks")]
[Authorize]
public async Task<IHttpActionResult> UpdateAllDataCaptureActivities(int activityId, [FromBody][ModelBinder] TaskDto tasks)
{
...
}
调用API时出现以下错误:
"Message": "An error has occurred.",
"ExceptionMessage": "Can't bind parameter 'tasks' because it has conflicting attributes on it.",
"ExceptionType": "System.InvalidOperationException",
答案 0 :(得分:8)
请勿同时使用[FromBody]和[ModelBinder]。