2012-07-06 48 views
1

我已经创建了一个WCF restful服务,它返回基本上是字符串的xml格式的组信息。为了得到这个信息,我需要传递两个参数,比如PersonId和GroupId - 都是字符串。这里一个人可以有多个小组。逻辑是,如果我同时传递PersonId和GroupId,那么它只会返回该组的具体信息,但如果我没有通过GroupId,那么方法将返回该人的所有组。到现在为止,我通过get方法使用这项服务,例如WCF Restful端点配置传递多个值

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

or 

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

界面中,就像如下:

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

,它是给我准确的结果是什么,我的预期。然后我尝试使其成为RESTFULL并在webInvoke中添加UriTemplate属性。例如

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

为了让我的服务问题的REST像

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

而且这是工作的罚款。 但现在我的问题已经开始。如果我没有设置GroupId,它会提供服务未找到或错误的请求错误。我想选择设置groupId。 例如

对于单组

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

和所有群体

localhost/service/service.svc/getGroupInfo/A100 

这可能吗?

等待您的宝贵回应..

谢谢。

+0

所有格式的检查:http://msdn.microsoft.com/en-us/library/bb675245.aspx – Luuk 2013-02-12 10:47:43

回答

1

你可以改变你的模板是 “getGroupInfo/{PERSONID}/{GroupId的= NULL}”,但我相信你还是会查询所有组

时需要在URL尾部的反斜杠
localhost/service/service.svc/getGroupInfo/A100/ 
+0

不知道,太好了! – Luuk 2013-02-12 10:46:50

0

你必须创建2种方法:

[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) 
{ 
} 

要使用可选参数,您必须使用'?'

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