我被这个非常奇怪的问题困住了。
我有一个名为AttendanceController
派生的 API 控制器,它又是从
这里APIControllerFA
派生的代码ApiController
public class AttendanceController : ApiControllerFA
{
public HttpResponseMessage PostAttendance([ModelBinder(typeof(AttendanceRegistrationModelBinder))]AttendanceRegistrationModel model)
{
//checking model state for errors
//throw new Exception("Just to throw an error ");
...........
从 PostAttendance 方法中可以看出,我有一个自定义ModelBinder
名称AttendenceRegistrationModelBinder
,这是代码
public class AttendanceRegistrationModelBinder : DefaultModelBinder
{
protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor)
{
if (propertyDescriptor.Name == "formJson")
{
string val = bindingContext.ValueProvider.GetValue(propertyDescriptor.Name).AttemptedValue;
dynamic jsonVal = JsonConvert.DeserializeObject(val);
propertyDescriptor.SetValue(bindingContext.Model, jsonVal);
return;
//SetProperty(controllerContext, bindingContext,propertyDescriptor,jsonVal);
}
base.BindProperty(controllerContext, bindingContext, propertyDescriptor);
}
}
但是当我尝试使用 Fiddler 访问这个控制器时。我收到一条错误消息
Could not create a 'IModelBinder' from 'AttendanceRegistrationModelBinder'. Please ensure it derives from 'IModelBinder' and has a public parameterless constructor
我在这里做错了什么?