Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two .NET Core 2.2 APIs both handling the same object in the same workflow (so they have the same auth token). The first one behaves as expected, the second refuses to fire giving Error: Model bound complex types must not be abstract or value types and must have a parameterless constructor.

This is the signature
[Authorize]
 [HttpPost("UpdateRegistration")]
 public async Task<IActionResult> UpdateRegistration(CUserDetails userDetails)
 {...}


This is the offending class:
   public class CUserDetails
    {

        #region CONSTRUCTORS

        /// <summary>
        /// Primary constructor
        /// </summary>
        /// <param name="arg_person">The details of the person to register</param>
        /// <param name="arg_username">the username to be assigned when creating the user</param>
        public CUserDetails(CPersonMasterBase arg_person, string arg_username)
        {
            this.person = arg_person;
            this.username = arg_username;

        }

        #endregion


        #region PUBLIC METHODS

        public string GetUsername()
        {
            return username;
        }

        public CPersonMasterBase GetPerson()
        {
            return person;
        }

        #endregion


        #region PUBLIC PROPERTIES
        public string username { get; set; }

        public CPersonMasterBase person { get; set; }

        #endregion
    }
}


The simple answer is to give it a parameterless constructor, but then I will not learn why it works in one case but not the other.

What I have tried:

So far just stepping in with the debugger, and examining the controller constructor. Found nothing to indicate why. I've done a search for userDetails in both solutions but found nothing to suggest why they are behaving differently.

At least one online solution just adds the parameterless constructor but that as I mentioned above does not explain the difference in behaviour.
Posted
Updated 17-Dec-19 7:21am
v2
Comments
Richard Deeming 17-Dec-19 13:05pm    
What's the signature of the one that works, and how is it different from the one that doesn't?
Ger Hayden 18-Dec-19 3:33am    
Hi Richard,
Both signatures were identical. The problem was a missing attribute at class level.

Ger

Is the CPersonMasterBase class you are using as the "person" property an abstract class? If not, does it also have a parameterless constructor?
 
Share this answer
 
Got it.

The offending class was missing the [ApiController] attribute. e.g.

[Route("CMDS/[Controller]")]
  [ApiController]
  public class DistributeController : ControllerBase
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900