繁体   English   中英

WCF Restful端点配置以传递多个值

[英]WCF Restful endpoint configuration to pass multiple values

我创建了WCF Restful服务,该服务以xml格式(基本上是字符串)返回组信息。 要获取此信息,我需要传递两个参数,例如PersonId和GroupId-都是字符串。 一个人在这里可以有多个组。 逻辑是如果我同时传递-PersonId和GroupId,则它将仅返回该组的特定信息,但是如果我不传递GroupId,则方法将返回该人的所有组。 到目前为止,例如,我正在通过get方法使用此服务

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);

使我的服务像RESTfull

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

而且工作正常。 但是现在我的问题开始了。 如果我没有设置GroupId,它会提供服务未找到或错误的请求错误。 我想可选地设置groupId。 例如

对于单个组

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

和所有组

localhost/service/service.svc/getGroupInfo/A100

可能吗?

等待您的宝贵回应。

谢谢..

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

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

您必须创建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)
{
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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