-1

I am passing an array of integers to ASP.NET Web API but I am receiving below error.

"An item with the same key has already been added."

This is my API endpoint:

[HttpGet]
[Route("api/Pen/ByPropertyIdList")]
public IQueryable<Pen> ListByPropertyIdList([FromUri] Int32[] propIds)
{
    Boolean? deleted = false;
    IQueryable < Pen > p = logic.ListByPropertyIdList(propIds, deleted).AsQueryable(); // I recieved the list of object perfectly
    return p; // returning them generates the error
}

This is the URL I use;

api/pen/ByPropertyIdList?propIds=12&propIds=348

The endpoint accepts my array of integers but at return point, it generates the error. I believe it is due to the propIds.

Keyur Ramoliya
  • 1,900
  • 2
  • 16
  • 17
iJay
  • 4,205
  • 5
  • 35
  • 63
  • 1
    *I recieved the list of object perfectly* no, you are not ... you are creating the query here ... – Selvin Oct 31 '19 at 09:53
  • Does this answer your question? [Pass an array of integers to ASP.NET Web API?](https://stackoverflow.com/questions/9981330/pass-an-array-of-integers-to-asp-net-web-api) – BugFinder Oct 31 '19 at 09:54
  • 1
    @BugFinder no, he is passing integers in the same way as its in the answer ... – Selvin Oct 31 '19 at 09:55
  • 1
    Why do you want to return a IQueryable ? ...Is there something oversea that can query the output without sending all the data ? :) – Laurent Lequenne Oct 31 '19 at 09:56
  • 1
    the real problem is inside `ListByPropertyIdList` or maybe when `IQueryable` (not results) is translated to JSON ... without full exception is hard to guess – Selvin Oct 31 '19 at 09:59
  • 1
    @Selvin sort of except it does explain that the receiving page depending on language can handle arrays differently – BugFinder Oct 31 '19 at 09:59

1 Answers1

1

Thank you @LaurentLequenne!

Changing IQueryable to IEnumerable resolve the problem.

[HttpGet]
[Route("api/Pen/ByPropertyIdList")]
public IEnumerable<Pen> ListByPropertyIdList([FromUri] Int32[] propIds)
{
    Boolean? deleted = false;
    IEnumerable< Pen > p = logic.ListByPropertyIdList(propIds, deleted).AsEnumerable();
    return p;
}
iJay
  • 4,205
  • 5
  • 35
  • 63