2

I have API controller that has one Put method

public class ScheduleExecutionsController : ApiController
{

    public ScheduleExecutionsResponse Put([ModelBinder(typeof(TestBinder))]ScheduleExecutionsRequest requestInfo)
    {
        ....
    }
}

I added a binder class to the project

public class TestBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        return new ScheduleExecutionsRequest();
    }
}

I set 2 breakpoints. First one to the first line of Put method in controller and second to first line of my TestBinder BindModel object. After with Fiddler I send PUT request.

Debugger stops always inside my action but never inside BindModel methof of the binder. It seems that default binder is used. What did I miss to add custom one?

Vitalii
  • 10,091
  • 18
  • 83
  • 151
  • Isn't BindModel supposed to return a bool instead of an object? – vc 74 Apr 06 '16 at 12:03
  • no, it should be an object https://msdn.microsoft.com/fr-fr/library/dd505073(v=vs.118).aspx – Vitalii Apr 06 '16 at 12:05
  • this link is for the Mvc documentation, the webapi doc is here https://msdn.microsoft.com/en-us/library/system.web.http.modelbinding.imodelbinder.bindmodel(v=vs.118).aspx#M:System.Web.Http.ModelBinding.IModelBinder.BindModel(System.Web.Http.Controllers.HttpActionContext,System.Web.Http.ModelBinding.ModelBindingContext) – vc 74 Apr 06 '16 at 12:07
  • Possible duplicate of [How do I create a custom model binder using the \`BindModel(HttpActionContext actionContext...\` signature?](https://stackoverflow.com/questions/9910346/how-do-i-create-a-custom-model-binder-using-the-bindmodelhttpactioncontext-act) – Liam May 18 '18 at 13:45

1 Answers1

4

Are you using the WebAPI or MVC version of ModelBinderAttribute?

Most of the infrastructure of MVC and WebAPI – filters, binding, etc. – exist in two forms (due to the history of the two libraries). Your WebAPI controllers and actions must use the WebAPI version of these (namespace System.Web.Http or its child namespaces)).

Richard
  • 106,783
  • 21
  • 203
  • 265
  • Thanks a lot. This was a problem of wrong namespace for IModelBinder at Binder calss and also with Model binder attribute that should look like this [System.Web.Http.ModelBinding.ModelBinder(typeof(TestBinder))] – Vitalii Apr 06 '16 at 12:17