3

I have an object called "ReferenceNumber". Reference numbers have a "Number" and a "Type".

The javascript object looks like this:

{
    "type" : ""
    "number" : ""
}

I need to send a list of these "ReferenceNumber"s to a C# System.Web.Http.ApiController. My method looks like this:

[HttpGet]
[Route(@"")]
public HttpResponseMessage MyMethod([FromUri]List<ReferenceNumber> refNumbers)
{ 
    ... 
}

I'm using $http from Angularjs to try to make all my API calls. I prefer to be able to do something like:

$http.get("MYURL", obj);

But I'm willing to use jQuery.param or JSON.stringify if need be. Or:

$http.get("MYURL", {params: {refNumbers: referenceNumbers}});

Or whatever. But no matter what combination of these things I try, including just hardcoding, I can't seem to get the list to be sent over. I can get a null list, an empty list, or a list with items in it with Number and Type properties that are null, but I can't get my data.

How can I send this data properly? Is sending a list simply not done? And, if so, why is a list not acceptable? Should I package my list in an object? Must I use [FromBody] instead of [FromUri] and force my data in there?

K_Ram
  • 367
  • 4
  • 14
  • I know Angular, but not ASP. What does you ASP service expect as query string? i.e. if you had to manually enter the URL to invoke this service with 2 reference numbers, what would you type? – JB Nizet Jul 27 '15 at 16:39
  • Beg pardon, I may have the wrong tag. It is an System.Web.Http ApiController. I'll try to fix the tag. – K_Ram Jul 27 '15 at 16:41
  • "referenceNumbers "is a list of ReferenceNumber objects, not a single object. Each object in the list has a name and type property. – K_Ram Jul 27 '15 at 16:44
  • try simply doing: `{params: referenceNumbers}` .... then url would be `?number=123&type=sometype` – charlietfl Jul 27 '15 at 16:44
  • Please see the above comment. I need a list of these objects, not just a single object. – K_Ram Jul 27 '15 at 16:46
  • @K_Ram why don't you answer my question? If you had to manually enter the URL to invoke this service with 2 reference numbers, what would you type? – JB Nizet Jul 27 '15 at 16:47
  • have u checked the url for the request, is it properly encoded, maybe it's broken somewhere – z.a. Jul 27 '15 at 16:52
  • @JB Nizet: I would have answered your question sooner had I realized you edited your original question. I would probably try "MYURL?refNums={type:TYPE1, number:1234}&refNums = { type:TYPE2, number:24423}" and so forth. – K_Ram Jul 27 '15 at 16:57
  • @z.a.: As I can hit the controller, I assume that my url is correct, but the parameters are apparently not being encoded in such a way that they are received. Hence my question. – K_Ram Jul 27 '15 at 16:58
  • you will need to create that string yourself or try as JSON.stringify. Can do it in $httpInterceptors if need to use it frequently. Would make more sense to post it though – charlietfl Jul 27 '15 at 17:01
  • so you want them urlencoded then. There are libraries for this. Jquery doesn't seem to work right. – z.a. Jul 27 '15 at 17:01
  • I've tried JSON.stringify and jQuery.param both and haven't had any success, unless I've used them terribly wrong. I've seen them work, used them myself, on complex objects, but not on lists. – K_Ram Jul 27 '15 at 17:16

1 Answers1

2

Your problem is probably solved by now, but I'm posting my findings for the people that might come across this question in the future.

This answer suggests to use the $httpParamSerializerJQLike param serializer. For me, it was the only way my C# back-end could understand that the parameter was an array of complex objects.

So in this specific case, you would have to do:

$http.get("MYURL", {params: {refNumbers: referenceNumbers}, paramSerializer: '$httpParamSerializerJQLike'});
Community
  • 1
  • 1
Chatonne
  • 64
  • 2
  • 11