I am learning .NET Core but I cannot figure how to do this.
Having this controller:
[Route("api/[controller]")]
[ApiController]
public class ModelController : Controller
{
private readonly ModelService _modelService;
public ModelController(ModelService ModelService)
{
_modelService= ModelService;
}
[HttpGet]
public ActionResult<ListModel>> Get() =>
_modelService.Get();
[HttpPost]
public ActionResult<Model> Create(Model newModel)
{
_modelService.Create(newModel);
return CreatedAtRoute("GetModel", new { id = model.Id.ToString() }, model);
}
}
This enum:
public enum ModelEnum
{
Property = 0,
OtherProperty = 1
}
And this model:
public class Model
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
[BsonElement("Name")]
public string Name{ get; set; }
public ModelEnum[] theEnum { get; set; }
}
How can can I send a request with the property theEnum? I am using postman to test it. When I try to do I receive this a HTTP 400 response with always with errors
First test: { "Name": "Test", "theEnum": [1, 2] }
First error response
{ "type": "https://www.rfc-editor.org/rfc/rfc7231#section-6.5.1", "title": "One or more validation errors occurred.", "status": 400, "traceId": "|100e6798-429066a034f499f7.", "errors": { "$.Clases": [ "The JSON value could not be converted to System.String. Path: $.Clases | LineNumber: 2 | BytePositionInLine: 21." ] } }
Second try:
{
"Name": "Test",
"theEnum": ["Property", "OtherProperty"]
}
Second error response:
{ "type": "https://www.rfc-editor.org/rfc/rfc7231#section-6.5.1", "title": "One or more validation errors occurred.", "status": 400, "traceId": "|100e6799-429066a034f499f7.", "errors": { "$.Escuelas[0]": [ "The JSON value could not be converted to Project.ModelEnum[]. Path: $.Escuelas[0] | LineNumber: 3 | BytePositionInLine: 29." ] } }
So I am wondering, how can I send the value of a Enum type array? Am I doing totally wrong? Maybe the validation messages said what I need but I cannot understand.
[JsonConverter(typeof(StringEnumConverter))] public enum ModelEnumusing Newtonsoft.Json.Converters;