我陷入了这个非常奇怪的问题。
我有名为AttendanceController
的API控制器从APIControllerFA
其inturn从ApiController
这里衍生衍生是代码在Asp.Net Web API中实现自定义模型绑定器时出错
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方法我有一个名为AttendenceRegistrationModelBinder
定制ModelBinder
指此是可见代码
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);
}
}
,但是当我尝试访问使用提琴手该控制器。我得到一个错误说
Could not create a 'IModelBinder' from 'AttendanceRegistrationModelBinder'. Please ensure it derives from 'IModelBinder' and has a public parameterless constructor
我在这里做错了什么?
+1表示使用JToken的建议 –