Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The type does not implement the IModelBinder interface. Parameter name: binderType

I have implemented custom model binder for my WebApi project

using WebApi.Controllers;
using System.Web.Http.ModelBinding;
using System.Web.Http.Controllers;

namespace WebApi.Models
{
    public class ModelBaseBinder : IModelBinder
    {
        public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
        {
            if ((bindingContext.Model is MyModel))
            {
                //my code here

                controller.InitModel(model);

                return true;
            }

            return false;
        }
    }
}

But for some reason in Global.asax.cs in line GlobalConfiguration.Configure(WebApiConfig.Register);I am getting an error that: The type does not implement the IModelBinder interface. Parameter name: binderType.enter image description here

my Global.asax.cs is looking like that:

AreaRegistration.RegisterAllAreas();            
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);

AutoMapperConfiguration.Configure();
GlobalConfiguration.Configuration.BindParameter(typeof(ModelBase), new ModelBaseBinder());
FluentValidationModelValidatorProvider.Configure(GlobalConfiguration.Configuration);

I am guessing that for some reason MVC is looking for System.Web.ModelBinding.IModelBinder but in case of WebApi I have System.Web.Http.ModelBinding.IModelBinder implementaton insteard.

Do you have an idea how can I fix that?

UPDATE:

I find out that if i comment this method on my controller:

public void Post([FromBody]MyModel model)
        {
            //my code here
        }

Than I am not getting the error above. But still cant understand why I an getting such issue.

Here is my models details

public class MyModel : ModelBase
    {
    }

[ModelBinder(typeof(ModelBaseBinder))]
public class ModelBase
    {
    }
like image 561
sreginogemoh Avatar asked Dec 14 '22 12:12

sreginogemoh


1 Answers

Verify if the ModelBinderAttribute on your model is the one from System.Web.Http.ModelBinding or System.Web.Mvc.

There are implementations of IModelBinder in both (System.Web.Http.ModelBinding and System.Web.Mvc) and your attribute match the namespace with your binder

like image 88
Pedro Drewanz Avatar answered Apr 15 '23 14:04

Pedro Drewanz