How to call Default Model Binding from custom binder in WebAPI?
Asked Answered
U

1

6

I have a custom model binder in WebAPI that uses the following method from the `Sytem.Web.Http.ModelBinding' namespace which is the correct namespace for creating custom model binders for Web API:

public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
{

}

I have a HTTP POST on a controller for which I want to use this custom model binder. The posted object contains roughly 100 fields. I want to change 2 of them. What I need is for default model binding to occur and then manipulate that model bound object for those 2 fields so that once the controller receives the object it is pristine.

The problem is I can't seem to model bind my object using the default binder from the model binding method above. In MVC there was the following:

base.BindModel(controllerContext, bindingContext);

This same approach does not work in WebAPI. Maybe I'm going about this wrong and there is another way to accomplish what I want so please suggest if a custom model binder is not the correct approach. What I'm trying to prevent doing is have to manipulate the posted object inside the controller. I could technically do that after it has been model bound, but I'm trying to do that earlier in the call stack so that the controller doesn't need to worry about the custom manipulation of those 2 fields.

How can I initiate default model binding against the bindingContext in my custom model binder so that I have a fully populated object where then I can just manipulate/massage the last 2 fields I need before returning?

Uppercase answered 8/4, 2016 at 16:35 Comment(1)
I realize it's several years later but I was wondering if you ever found a solution to this? I'm facing the same issue now. @UppercaseHospitium
A
4

In WebApi the 'default' model binder is CompositeModelBinder which wraps all registered model binders. If you want to re-use it's functionality you could do something like:

public class MyModelBinder : IModelBinder
{
    public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
    {
        if (bindingContext.ModelType != typeof(MyModel)) return false;

        //this is the default webapi model binder provider
        var provider = new CompositeModelBinderProvider(actionContext.ControllerContext.Configuration.Services.GetModelBinderProviders());
        //the default webapi model binder
        var binder = provider.GetBinder(actionContext.ControllerContext.Configuration, typeof(MyModel));

        //let the default binder do it's thing
        var result = binder.BindModel(actionContext, bindingContext);
        if (result == false) return false;

        //TODO: continue with your own binding logic....
    }
}
Addington answered 5/8, 2016 at 13:3 Comment(3)
I'm trying to figure out how to exclude the existing MyModelBinder from just calling itself, since it's the default binder for MyModel.Archon
Did you ever find a solution for this?Fount
did you @ntziolis?Hospitium

© 2022 - 2024 — McMap. All rights reserved.