title: ASP.NET Core MVC - Model Binding : Bind an interface model using the attribute [FromBody] (BodyModelBinder)
body: <p>I want to bind an interface model from my action method with a request that the content-type is application/json. I'm using the [FromBody] attribute in my action method.</p>
  
  <p>I tried to create a custom modelBinder derived from ComplexTypeModelBinder, by following this link: <a href="http://www.dotnet-programming.com/post/2017/03/17/Custom-Model-Binding-in-Aspnet-Core-2-Model-Binding-Interfaces.aspx" rel="nofollow noreferrer">Custom Model Binding in Asp.net Core, 3: Model Binding Interfaces</a>, but it doesn't work, my model is always null. I learned after that when you use the atribute [FromBody] the BodyModelBinder is called and internally is calling JsonInputFormatter and it doesn't use the custom modelBinder.</p>
  
  <p>I'm looking for a way to bind my interface model. I can use the MVC DI to map each interface with its implementation. My action method is defined as :</p>
  
  <pre><code>public async Task&lt;IActionResult&gt; Create(IOperator user)
      {
          if (user == null)
          {
              return this.BadRequest("The user can't not be null");
          }
  
          if (!this.ModelState.IsValid)
          {
              return this.BadRequest(this.ModelState);
          }
  
              IOperator op = await this.AuthenticationFrontService.CreateOperatorAsync(user.Login, user.Password, user.FirstName, user.LastName, user.ValidUntil, user.Role, user.Comment);
          return new CreatedAtActionResult("Get", "operators", new { id = ((Operator)op).Id }, op);
      }
  </code></pre>
  
  <p>I tried another solution by using the MetadataType attribute in my interface but it doesn't exist in the namespace System.ComponentModel.DataAnnotations and i read that asp.net core mvc doesn't use this attribute <a href="https://stackoverflow.com/questions/34576921/asp-net-mvc-metadatatype-attribute-not-working">Asp.Net MVC MetaDataType Attribute not working</a>. I don't want to install the package microsoft.aspnetcore.mvc.dataannotations in domain model project to use the ModelDataType attribute.</p>
  
  <p>I tried another solution by creating a custom JsonInputFormater in other words i derived  the class JsonInputFormatter and by analyzing the source code, i've found that the JsonSerializer couldn't deserialize an interface which is logical. So i'm looking for a solution where i could custom the jsonserializer maybe by using a resolver or a generic converter.</p>
  
  <p>Any help will greatly appreciated.</p>
  
  <p>Thanks.</p>