简体   繁体   English

WebAPI ModelBinder错误

[英]WebAPI ModelBinder Error

I've implemented a ModelBinder but it's BindModel() method is not being called, and I get Error Code 500 with the following message: 我已经实现了一个ModelBinder但它的BindModel()方法没有被调用,我得到错误代码500,并带有以下消息:

Error: 错误:

Could not create a 'IModelBinder' from 'MyModelBinder'. 无法从“MyModelBinder”创建“IModelBinder”。 Please ensure it derives from 'IModelBinder' and has a public parameterless constructor. 请确保它派生自'IModelBinder'并具有公共无参数构造函数。

I do derive from IModelBinder and do have public parameterless constructor. 我从IModelBinder派生,并有公共无参数构造函数。

My ModelBinder Code: 我的ModelBinder代码:

public class MyModelBinder : IModelBinder
    {
        public MyModelBinder()
        {

        }
        public bool BindModel(ModelBindingExecutionContext modelBindingExecutionContext, ModelBindingContext bindingContext)
        {
            // Implementation
        }
    }

Added in Global.asax: 在Global.asax中添加:

protected void Application_Start(object sender, EventArgs e)
{
    ModelBinders.Binders.DefaultBinder = new MyModelBinder();

    // ...
}

WebAPI Action Signature: WebAPI行动签名:

    [ActionName("register")]
    public HttpResponseMessage PostRegister([ModelBinder(BinderType = typeof(MyModelBinder))]User user)
    {
        return new HttpResponseMessage(HttpStatusCode.OK);
    }

User Class: 用户类:

public class User
{
    public List<Communication> Communications { get; set; }
}

ASP.NET Web API uses a completely different ModelBinding insfracture than APS.NET MVC. ASP.NET Web API使用与APS.NET MVC完全不同的ModelBinding insfracture。

You are trying to implement the MVC's model binder interface System.Web.Mvc.IModelBinder but to work with Web API you need to implement System.Web.Http.ModelBinding.IModelBinder 您正在尝试实现MVC的模型绑定器接口System.Web.Mvc.IModelBinder但要使用Web API,您需要实现System.Web.Http.ModelBinding.IModelBinder

So your implementation should look like this: 所以你的实现应该是这样的:

public class MyModelBinder : System.Web.Http.ModelBinding.IModelBinder
{
    public MyModelBinder()
    {

    }

    public bool BindModel(
        System.Web.Http.Controllers.HttpActionContext actionContext, 
        System.Web.Http.ModelBinding.ModelBindingContext bindingContext)
    {
        // Implementation
    }
}

For further reading: 进一步阅读:

This for using System.Web.ModelBinding 这适用于使用System.Web.ModelBinding

 using System.Web.ModelBinding;
class clsUserRegModelBinder : IModelBinder
{
   public bool BindModel(ModelBindingExecutionContext modelBindingExecutionContext, ModelBindingContext bindingContext)
   {
        throw new NotImplementedException();
   }
}

This for System.Web.MVC 这适用于System.Web.MVC

using System.Web.Mvc;


class clsUserRegModelBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext,     ModelBindingContext bindingContext)
    {
        throw new NotImplementedException();
    }
}

Note the Different i hope it help you 注意不同我希望它能帮到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM