C#Web.API无法添加自定义模型绑定器

C#Web.API无法添加自定义模型绑定器,c#,asp.net-web-api,model-binding,custom-model-binder,C#,Asp.net Web Api,Model Binding,Custom Model Binder,我有一个API控制器,它有一个Put方法 public class ScheduleExecutionsController : ApiController { public ScheduleExecutionsResponse Put([ModelBinder(typeof(TestBinder))]ScheduleExecutionsRequest requestInfo) { .... } } 我在项目中添加了一个活页夹类 public clas

我有一个API控制器,它有一个Put方法

public class ScheduleExecutionsController : ApiController
{

    public ScheduleExecutionsResponse Put([ModelBinder(typeof(TestBinder))]ScheduleExecutionsRequest requestInfo)
    {
        ....
    }
}
我在项目中添加了一个活页夹类

public class TestBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        return new ScheduleExecutionsRequest();
    }
}
我设置了2个断点。控制器中Put方法第一行的第一个和TestBinder BindModel对象第一行的第二个。 和提琴手一起后,我发出放琴请求

调试器总是在我的操作中停止,但从不在活页夹的BindModel方法中停止。似乎使用了默认的活页夹。添加自定义项时我错过了什么

您是否正在使用或版本的
ModelBinderAttribute


MVC和WebAPI的大部分基础设施——过滤器、绑定等——以两种形式存在(由于这两个库的历史)。您的WebAPI控制器和操作必须使用这些的WebAPI版本(名称空间
System.Web.Http
或其子名称空间))。

BindModel不应该返回bool而不是对象吗?不,它应该是一个对象此链接用于Mvc文档,WebAPI文档在此可能重复,非常感谢。这是Binder Cals中IModelBinder的名称空间错误的问题,也是Model Binder属性的问题,该属性应如下所示[System.Web.Http.ModelBinding.ModelBinder(typeof(TestBinder))]