[英]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.