1

I couldn't find much about this in the documentation, all of the examples has primitive types in their input, like int id.

I did find an example though of someone passing a Date, and i was thinking that maybe the controller knows how to parse a UNIX timestamp?

and i tried the following:

    [HttpGet]
    public async Task<IActionResult> Get([FromQuery] IEnumerable<int> ids)
    {
        IEnumerable<User> userIds = await _userRepository.FindByAsync(user => ids.Contains(user.Id), "");
        if (userIds == null) return NotFound();
        IEnumerable<UserDto> userDtos = players.Select(Mapper.Map<UserDto>);
        return Json(userDtos);
    }

But i got invalid modelState when i tried to call it with /api/users/get?ids=1,2,4,5,6

So it doesn't know how to parse a IEnumerable? so i have a few concrete questions:

  1. Should i just take in a collection as a string, split on the ',' and cast it to ints? should i create some middleware that does this validation/transformation before it even hits the controller or?

2.How does someone put in a Date field in the controller? is it documented anywhere what complex types is supported? Or did the persons example code i saw do something extra behind the scenes?

DenLilleMand
  • 3,732
  • 5
  • 25
  • 34
  • /api/users/get?ids=1&ids=2&ids=3 .. and how does this sounds ? Otherwise you need to write your own model binder. – Laurent Lequenne Jan 23 '17 at 13:04
  • 1
    check this link : http://stackoverflow.com/questions/9584573/model-binding-comma-separated-query-string-parameter – Laurent Lequenne Jan 23 '17 at 13:05
  • Try passing the Enumerable like this `get?ids[0]=1&ids[1]=2&ids[2]=4`. That is how you pass them for `FromForm` – Mats391 Jan 23 '17 at 13:06
  • Consider using POST method and pass that ids as list of JSON objects or whatever you like. – Yuriy Ivaskevych Jan 23 '17 at 13:08
  • Laurent Lequenne - Thanks for answering, obviously my question is a complete duplicate of that one, and the answers looks pretty much like what i expected :) Thanks. – DenLilleMand Jan 23 '17 at 14:09
  • Yuriy Ivaskevych - I think it would be more simple that way, but it would make the API harder to reason about, querystrings is pretty standard in a restful API combined with get calls, and i think it makes more sense when the client just want to "get" something. – DenLilleMand Jan 23 '17 at 14:11

0 Answers0