自定义模型绑定程序:无法绑定参数'xxx',因为它具有冲突的属性

时间:2017-05-17 12:44:07

标签: asp.net-web-api asp.net-web-api2 model-binding custom-model-binder

我正在使用自定义模型绑定器,以便在我的模型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",

1 个答案:

答案 0 :(得分:8)

请勿同时使用[FromBody]和[ModelBinder]。