的WebAPI ModelBinder的错误 [英] WebAPI ModelBinder Error

查看:408
本文介绍了的WebAPI ModelBinder的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了一个 ModelBinder的但它的 BindModel()方法不会被调用,而我得到的错误code 500以下消息:

错误:

无法
创建从MyModelBinder'一'IModelBinder。请确保它派生
从IModelBinder'和具有公共的无参数
构造函数。

我从IModelBinder派生,并确实有公共参数的构造函数。

我的ModelBinder的code:

 公共类MyModelBinder:IModelBinder
    {
        公共MyModelBinder()
        {        }
        公共BOOL BindModel(ModelBindingExecutionContext modelBindingExecutionContext,ModelBindingContext的BindingContext)
        {
            //实施
        }
    }

添加在Global.asax中:

 保护无效的Application_Start(对象发件人,EventArgs的发送)
{
    ModelBinders.Binders.DefaultBinder =新MyModelBinder();    // ...
}

的WebAPI操作签名:

  [ActionName(注册)
    公众的Htt presponseMessage的postRegister([ModelBinder的(BinderType = typeof运算(MyModelBinder))]用户用户)
    {
        返回新的Htt presponseMessage(的HTTPStatus code.OK);
    }

用户等级:

 公共类用户
{
    公开名单<通信与GT;通信{搞定;组; }
}


解决方案

的ASP.NET Web API使用完全不同的ModelBinding insfracture比APS.NET MVC。

您正试图实现MVC的模型绑定接口 System.Web.Mvc.IModelBinder 但随着网络的API来工作,你需要实施系统.Web.Http.ModelBinding.IModelBinder

所以,你的实现应该是这样的:

 公共类MyModelBinder:System.Web.Http.ModelBinding.IModelBinder
{
    公共MyModelBinder()
    {    }    公共BOOL BindModel(
        System.Web.Http.Controllers.HttpActionContext ActionContext中,
        System.Web.Http.ModelBinding.ModelBindingContext的BindingContext)
    {
        //实施
    }
}

有关进一步阅读:

I've implemented a ModelBinder but it's BindModel() method is not being called, and I get Error Code 500 with the following message:

Error:

Could not create a 'IModelBinder' from 'MyModelBinder'. Please ensure it derives from 'IModelBinder' and has a public parameterless constructor.

I do derive from IModelBinder and do have public parameterless constructor.

My ModelBinder Code:

public class MyModelBinder : IModelBinder
    {
        public MyModelBinder()
        {

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

Added in Global.asax:

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

    // ...
}

WebAPI Action Signature:

    [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.

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

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:

这篇关于的WebAPI ModelBinder的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆