1

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.

4
  • you need to have [JsonConverter(typeof(StringEnumConverter))] public enum ModelEnum Commented Jun 11, 2020 at 12:41
  • I guess that StringEnumConverter is a class that I have to write, is this correct? Commented Jun 11, 2020 at 12:45
  • No, Its in using Newtonsoft.Json.Converters; Commented Jun 11, 2020 at 12:47
  • But I dont think it will work in .net core project. It works with .net framework Commented Jun 11, 2020 at 12:49

2 Answers 2

1

You test value for enum 1 and 2. but don't provide enum for value 2
You provide

public enum ModelEnum
{
    Property = 0,
    OtherProperty = 1
}

Property with value 0 and OtherProperty with value 1. but test value 1 and 2. Add MyProperty = 2 in ModelEnum or test 0,1 value
for secon try you need use StringEnumConverter

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, cant understand you. Can you explain your answer?
Thanks for the edit. Now I understand you. I will try it.
0

I guess the problem is about serializing enum array to JSON. In Core 3.0+ there is a new serializer that does not yet fully support serialization of enums. What you need to do is to add Newtonsoft.Json to your project. And then call AddNewtonsoftJson() in Startup.ConfigureServices.

Note that enum array converts to digits during serialization

1 Comment

Thanks for it, I will give it a try

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.