I'm trying to do a basic HTTP Post to an ASP.NET Core back end.
Angular:
var parms = {
userRoles: ['User', 'Worker'],
email: '[email protected]'
};
this.http.post('/UserRole/SaveUserRoles', parms)
.subscribe(result => { });
Controller:
[HttpPost]
public async Task<IActionResult> SaveUserRoles([FromBody]string[] userRoles, string email)
{
return Ok();
}
My parameters are showing null. I can get this working by creating a complex object C# side, but that's not what I want to do. I don't need an object just for 2 parameters.
What's missing here?
FromBody[FromBody] parmsand that object would have the userRoles and email properties.[FromBody]isn't selective - you can't bind to a specific JSON property from the input object like you're trying to do here.[FromBody]will just attempt to bind the input against the type being decorated, which fails in your case as there's no match between an object (the input) and an array (the C# type).[FromBody]attribute is designed to be used.