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?

12
  • 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? Commented Jul 27, 2015 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. Commented Jul 27, 2015 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. Commented Jul 27, 2015 at 16:44
  • try simply doing: {params: referenceNumbers} .... then url would be ?number=123&type=sometype Commented Jul 27, 2015 at 16:44
  • Please see the above comment. I need a list of these objects, not just a single object. Commented Jul 27, 2015 at 16:46

1 Answer 1

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'});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.