简体   繁体   中英

WCF Restful endpoint configuration to pass multiple values

I have created a WCF restful service that returns group information in xml format that is basically a string. To get this information I need to pass two parameters like PersonId and GroupId - both are string. Here a person can have more than one groups. The logic is if I pass both - PersonId and GroupId then it will return the specific information of that group only, but if I do not pass the GroupId then method will return all the groups of that person. Up to now I was using this service via get method, for example

localhost/service/service.svc/getGroupInfo?PersonId=A100&GroupId=E100

or 

localhost/service/service.svc/getGroupInfo?PersonId=A100&GroupId=

The interface is like below :

[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
string getGroupInfo(string PersonId, string GroupId);

and it was giving me accurate result what I was expected. Then I tried to make it RESTFull and added UriTemplate attribute in the webInvoke . For example

[OperationContract]
    [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare,UriTemplate = "getGroupInfo/{PersonId}/{GroupId}")]
    string getGroupInfo(string PersonId, string GroupId);

To make my service RESTfull like

localhost/service/service.svc/getGroupInfo/A100/E100

And it is working fine. But now my problem has started. If I do not set the GroupId, it gives service not found or bad request error. I want to set groupId optionally. For example

For single group

localhost/service/service.svc/getGroupInfo/A100/E100

and for all groups

localhost/service/service.svc/getGroupInfo/A100

Is it possible?

Awaiting for your valuable response..

Thank you..

您可以将模板更改为“ getGroupInfo / {PersonId} / {GroupId = null}”,但我相信在查询所有组时,URL中仍需要使用反斜杠

localhost/service/service.svc/getGroupInfo/A100/

You'll have to create 2 methods:

[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare,UriTemplate = "getGroupInfo/{PersonId}")]
string getGroupInfo(string PersonId)
{
    return getGroupInfo(PersonId, null);
}

[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare,UriTemplate = "getGroupInfo/{PersonId}/{GroupId}")]
string getGroupInfo(string PersonId, string GroupId)
{
}

To use optional parameters you'll have to use a '?'

[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare,UriTemplate = "getGroupInfo/{PersonId}?GroupId={GroupId}")]
string getGroupInfo(string PersonId, string GroupId)
{
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM