I am working on sample application to get employment data from a server using a REST API.
[OperationContract]
[WebGet(UriTemplate = "/employ?id={empIDs}", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
List GetEmpList(string empIDs);
To get the employ details I called it and it worked fine.
GetEmpList("1");
Above code takes only one ID, but I want multiple employ details. Then to get multiple employ I need to use URL <root>/employ?1d=1&id=41&id=45
But to solve this I called the GetEmpList()
API as below
GetEmpList("1&id=41&id=45");
But it give me an exception:
System.ServiceModel.EndpointNotFoundException
Message :
There was no endpoint listening at https://sample.com/rest/employ?id=221%26id%3d211%26id%3d%26id%3d1057%26id%3d%26id%3d445 that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.
If I hard code URL as
[OperationContract]
[WebGet(UriTemplate = "/employ?id={empIDs}&id={empIDs2}&id={empIDs3}", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
List GetEmpList(string empIDs, string empIDs2, string empIDs3);
then it works, but the problem is number of employ varies based on request.
My question is how to pass multiple parameter to an UriTemplate
?
There is no standard way to pass in an array-type argument in a query string, but you can do so yourself fairly easily. Assuming all your employee IDs are going to be integers, probably the easiest way (from a technical and a human-readable standpoint) is to pass them in as a comma-delimited string.
Example (unchanged from your original code):
[OperationContract]
[WebGet(UriTemplate = "/employ?id={empIDs}", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
List GetEmpList(string empIDs);
Call:
GetEmpList("1,2,13");
In the method implementation, you simply do:
var ids = empIDs.Split(new [] { ',' }).Select(id => Int32.Parse(id));
See also this answer .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.