简体   繁体   中英

c# rest api only getting first value parameter list

I'm looking for a way to make my REST Api in C# so it will be able to work with request like this:

http://localhost:1234/api/Order/GetWithParam?orderIdString=4&orderIdString=7

Right now I have this in my controller:

[HttpGet]
[Microsoft.AspNetCore.Mvc.ProducesResponseType(typeof(Order), 200)]
[Route("api/Order/GetWithParam")]
public List<Order> GetDataWithParam(string orderIdString = null, DateTime? startDate = null, DateTime? endDate = null, string orderDescriptorsIdString = null)
    {
      ...

This works fine if I have one or zero arguments for my parameter:

http://localhost:1234/api/Order/GetWithParam?orderIdString=4

orderIdString will be equal to "4"

But if I send more than one argument for a parameter, only the first one will be taken:

http://localhost:1234/api/Order/GetWithParam?orderIdString=4&orderIdString=7

orderIdString will be equal to "4"... but it should be equal to "4,7".

What am I missing?

-- EDIT --

I tried to change:

string orderIdString = null

to

string[] orderIdString

but now orderIdString is null even when there are parameters passed

http://localhost:1234/api/Order/GetWithParam?orderIdString=4&orderIdString=7

will give me orderIdString == null

If you want to bind multiple parameter values you should make the type an array ( string[] ):

[HttpGet]
[Microsoft.AspNetCore.Mvc.ProducesResponseType(typeof(Order), 200)]
[Route("api/Order/GetWithParam")]
public List<Order> GetDataWithParam([FromQuery]string[] orderIdString = null, DateTime? startDate = null, DateTime? endDate = null, string orderDescriptorsIdString = null)
    {
      ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM