Model binding in ASP.NET Core is different than ASP.NET as discussed here. Even in ASP.NET your controller doesn't seem like it should work properly.
The binder attempts to match parameters to the received data via the URL, form values, etc. Any that match are assigned to parameters. The FromBody
attribute tells the binder to look at the body of the request to match the data. Hence in your case it would look in the body of the request. But the assumption here is that it is a model so the parameter name itself is irrelevant. Since string
doesn't have any properties it wouldn't try to match anything. The other 2 parameters are not specified as FromBody
, because only 1 parameter supports this, and therefore would never be bound. AFAIK this wouldn't even have worked in ASP.NET but honestly I never would have tried it anyway.
The correct approach to storing data in the body and mapping multiple values to it is to define a model type with the fields inside. Then use the model as a parameter with the FromBody
attribute.
public class PersonInfoModel
{
public string Name { get; set; }
public int Age { get; set; }
public string SSN { get; set; }
}
public string personInfoPost ( [FromBody] PersonInfoModel model )
{
}