2

I'm working on an REST API in ASP.NET. One of the first things I made was a Model Binder for the model, binded with the model by a code like this:

[ModelBinder(typeof(MyModelModelBinder))]
public class MyModel { ... }

And used it in an action

public IHttpActionResult Post(MyModel model) { ... }

I realised, quite suddenly, that after I added the [FromBody] before the parameter type...

public IHttpActionResult Post([FromBody]MyModel model) { ... }

...the Model Binder gets ignored. I tried to debug it by puting a breakpoint on the beginning of the BindModel method and at the beginning of the action and realised that when I call the action I get straight to the action. When I delete the FromBody Attribute the breakpoint in the ModelBinder becomes active again.

Why can't I use both? Why is the FromBody attribute ignoring the ModelBinder?

Maurice Klimek
  • 930
  • 5
  • 13
  • 48
  • 3
    I don't think you can use both a FromBody (which is base on `HttpParameterBinding`) and a ModelBinder. According to [this](http://www.asp.net/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api]) you can override the default policy by implementing an `IActionValueBinder ` – Michael Sep 25 '16 at 18:57
  • 1
    I guess this is the article that @Michael mentioned https://www.asp.net/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api – Wahid Bitar Jan 13 '17 at 06:12

0 Answers0