I'm making a simple API in .net core and am trying to realise a post, where i need to handle an enum value.
I'm using a "SaveProfileResource" for that, which is beeing used by AutoMapper in my Controller class.
what i specifically want is making a post request with a body like this:
{
"FirstName": "Karl",
"LastName": "Marx",
"UserName": "MarxDidNothingWrong69",
"Gender": "Diverse"
}
where Gender is a Enum.
code looks something like this:
public class SaveProfileResource
{
[Required]
[MaxLength(60)]
public string FirstName { get; set; }
[Required]
[MaxLength(60)]
public string LastName {get; set;}
[Required]
[MaxLength(60)]
public string UserName {get; set;}
[Required]
public EGender Gender {get; set;}
}
where EGender looks like this:
public enum EGender
{
[Description("Male")]
Male = 1,
[Description("Female")]
Female = 2,
[Description("Diverse")]
Diverse = 3,
}
and the post method in my controller class:
[HttpPost]
public async Task<IActionResult> PostAsync([FromBody] SaveProfileResource resource){
if(!ModelState.IsValid){
return BadRequest(ModelState.GetErrorMessages());
}
var profile = mapper.Map<SaveProfileResource, Profile>(resource);
var result = await profileService.SaveAsync(profile);
if(!result.Success){
return BadRequest(result.Message);
}
var profileResource = mapper.Map<Profile, ProfileResource>(result.Profile); //displaying the result to the user
return Ok(profileResource);
}
with my current "raw" implementation im getting a
"The JSON value could not be converted to x.y.z.Models.EGender. Path: "
error. My Question is:
what do i need to do for me to be able to send a request containing the Gender Enum Description.