Click here to Skip to main content
14,981,456 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
This is my class :
C#
<pre>[DataContract]
public class UserIdParams 
{
    [DataMember]
    public int UserId { get; set; }
    [DataMember]
    public List<int> ListUserId { get; set; }
}


it contains both a list of integer and an integer.

My goal is create the service WCF REST : GetUserByID to get list of users according to ids.


but as we know , we can't pass Array or complex types as input parameter. in wcf rest ,

(and when I test it like that, I have this error:

error-Query-String-Convertor — imgbb.com[^]

)

In other hand , it worked fine for WCF SOAP.

So any idea how to resolve my problem to get all users with WCF REST and the input parameter is an array ?

thanks,

What I have tried:

C#
[OperationContract]
  [WebGet]
  List<User> GetUserByID (UserIdParams userIdParams);
Posted
Updated 25-Sep-19 2:18am
Comments
F-ES Sitecore 24-Sep-19 5:59am
   
I googled "wcf rest array parameter" and found a few possible solutions. Would probably be quicker to just google it yourself and go through them.
Member 14601214 24-Sep-19 6:13am
   
sorry but i didn't find any solution fot this problem , and I try my code ..always the same problem..
In fact , all parameter is ok with rest but for input array parameter , it is very complicate
F-ES Sitecore 25-Sep-19 8:34am
   
Really? I just googled "wcf rest array parameter" and the second result was suggesting what was posted in Solution 1.

1 solution

Usually GET operations support parameters on the path or the query string only so they're not suitable for complex types such as Lists or Arrays.

On the other hand you can pass a collection as a body parameter to a POST operation.


So you would have to either change the operation to a POST, or make the argument a delimited string that you can process on the server side to produce the array

C#
[DataContract]
public class UserIdParams 
{
    [DataMember]
    public int UserId { get; set; }
    [DataMember]
    public string DelimitedUserIds { get; set; }
}

// Then on Server do something like 

string DelimitedUserIds = "9,3,12,43,2"
List<int> UserIds = DelimitedUserIds .Split(',').Select(int.Parse).ToList();


Again I would suggest to make it a POST and send params in the body.
   
Comments
Member 14601214 25-Sep-19 8:19am
   
Thanks a lot dnxit !! it works like a magic !

In fact, I do the delimited string and still with GET , i get all the users which I want from a list of Ids,
dnxit 25-Sep-19 8:44am
   
you're welcome, good to know your problem got a solution :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900