3

I'm trying to pass a list parameter to an ASP.NET Core 5 Web API.

Here is my endpoint:

[ApiController]
[Route("[controller]")]
public class InvoiceController : ControllerBase
{
 private readonly IConfiguration _configuration;
 public InvoiceController(IConfiguration configuration)
 {
 _configuration = configuration;
 }
 [HttpPost]
 public List<CustomerInvoice> Post(string[] CustomerNumbers, DateTime OrderDateFrom, DateTime OrderDateTo)
 {
 }
}

Sending json post request fails with this error:

The JSON value could not be converted to System.String[]. Path: $ | LineNumber: 0 | BytePositionInLine: 1.

and here is the request:

{
 "CustomerNumbers": ["test"],
 "OrderDateFrom": "2021年01月01日",
 "OrderDateTo": "2021年11月02日"
}

Instead of using a string array, I also tried List<string>. I got the same error.

Any idea why the framework does not understand how to receive a list?

marc_s
759k185 gold badges1.4k silver badges1.5k bronze badges
asked Dec 30, 2021 at 20:20
1
  • 2
    What's the JSON value look like? Commented Dec 30, 2021 at 20:22

1 Answer 1

3

You should create a class and receive JSON with this class. This may solve your problem.

public class ClassExample
{
 public List<string> CustomerNumbers { get; set; }
 public DateTime OrderDateFrom { get; set; }
 public DateTime OrderDateTo { get; set; }
}
[HttpPost]
public List<CustomerInvoice> Post(ClassExample requestParameter)
{
}
Jeremy Caney
7,790113 gold badges56 silver badges86 bronze badges
answered Dec 30, 2021 at 21:03

1 Comment

Great. This does work. Thanks. My comment documentations do not appear in swagger with this approach. Do you know if there is a documentation on this?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.