In Swagger I insert some values and I want to pass an array of strings, containing those values, to Linq. (the ids are strings in this case). If I pass a simple string, not an array, it works to insert one value. But I need to pass an array of values.
Here's a link built by the API when trying to insert 2 values (it looks weird for an array): http://localhost:port/api/Members?ids=97882831302&ids=97882831308
The problem is that the array shows NULL instead of the values inserted. And I get the following error:
"ExceptionMessage": "Unable to create a null constant value of type 'System.String[]'. Only entity types, enumeration types or primitive types are supported in this context."
public class MembersController : ApiController
{
public HttpResponseMessage GetAllMembersByIdentifiers(string[] ids) {
using (sampleEntities entities = new sampleEntities()){
var numbers = entities.tblmembers
.Where(p => ids.Contains(p.identify) ).ToList();
if (numbers != null)
{
return Request.CreateResponse(HttpStatusCode.OK, numbers);
}
else
{
return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Member with id: " + ids + " not found");
}
}
}
}
o think you've 2 problems in your code ..
FIRST:
pass data with , like:
SECOND:
in your C# can i suggest to you to check for string null or empty like:
[FromUri]
did the fix in my case. Collection parameter type can beIEnumerable<...>
.It's confusing that other parameters are filled correctly from a URL by default - which IMO should also happen to collection parameters as it's a get request.
You could check whether the parameters are passed by inspecting
Request.RequestUri.AbsoluteUri
.With
[FromUri]
:Without
[FromUri]
:What solved it was:
adding
[FromUri]
hinted by @federico-scamuzzireplacing
string[]
withList<string>
.So in the code looks like this: