0

I build an API using .Net Framework , I have a controller

[RoutePrefix("api/student")]
public StudentController : ApiController
{
    private readonly IService service;
}

public StudentController(IService service)
{
   this.service = service
}

[HttpGet, Route("getStudents/{Ids:int}")]     
public async Task<IHttpActionResult> GetStudents([FromUri]GetStudentsRequest request)
{
   //bla bla bla 
}

and the class GetStudentsRequest is

public class GetStudentsRequest 
{
   public int[] Ids { get; set; }
}

I want to pass many ids in a single request, but the problem is how to pass for example 3 ids via postman??

Or I must change the attribute?

I want only from uri not from body!

I try this Pass an array of integers to ASP.NET Web API? the highest ranking answer localhost:XXXXX/api/student/getStudents?Ids=1&Ids=2 but did not work. The different is that I have an object which have property the array.

dimmits
  • 1,999
  • 3
  • 12
  • 30
  • as i know `GET` Requests looks like that `http://0.0.0.0?var1=value1&var2=value2` so give me example of how u will pass array in the url and i will help you. – CorrM Nov 14 '19 at 07:53
  • yes i try http://localhost:XXXXX/api/student/getStudents?Ids=1&Ids=2 but this uri cannot reach the contructor – dimmits Nov 14 '19 at 07:55
  • You coded `Route` class .? if yes, i think u need to add to it to the `question`. – CorrM Nov 14 '19 at 08:01
  • nope ,they are the default attributes – dimmits Nov 14 '19 at 08:02

2 Answers2

0

The problem is you are trying to bind an object and not a list.

Try to change your signature like this:

public async Task<IHttpActionResult> GetStudents([FromUri]int[] ids)

Ygalbel
  • 5,214
  • 1
  • 24
  • 32
0

Finally i solve it . for future reference if someone have the int array as property in a request object,must modify the

Route attribute

the correct is

[HttpGet, Route("getStudents")]  //i remove /{Ids:int} 
public async Task<IHttpActionResult> GetStudents([FromUri]GetStudentsRequest request)
{
   //bla bla bla 
}

and the uri is localhost:XXXXX/api/student/getStudents?Ids=1&Ids=2

if someone has a better solution please post it!

dimmits
  • 1,999
  • 3
  • 12
  • 30