[英]Unable to pass a Dictionary as a parameter to WCF REST service in GET type of method
我有一个要求,其中大多数 WCF REST api 都是 GET 类型,并且会接收 Dictionary 作为参数。 但是当它如下放置时,它会引发错误。
[WebGet(UriTemplate = "/GetVersion?Param1={serverDetails}")]
public Version GetVersion(Dictionary<string, string> keyValue)
{
}
它给出以下错误:
合约“IDeploy”中的“GetVersion”操作有一个名为“keyValue”的查询变量,类型为“System.Collections.Generic.Dictionary'2[System.String,System.String]”,但类型为“System.Collections.Generic.Dictionary” '2[System.String,System.String]' 不能被 'QueryStringConverter' 转换。 UriTemplate 查询值的变量必须具有可由“QueryStringConverter”转换的类型。
知道如何解决这个问题吗? 很难替换 Dictionary 参数类型,因为服务中有很多这样的方法。
对于支持如此复杂的参数, Get
请求无法完成。 我们需要Post/Put/Delete
请求来完成此任务,因为我们可以在请求正文中设置这些复杂的参数。
[OperationContract]
[WebInvoke(RequestFormat =WebMessageFormat.Json,BodyStyle =WebMessageBodyStyle.Bare)]
string GetData(Dictionary<string,string> value);
以下是样品请求的 JSON 内容。
[{
"Key":"Key1",
"Value":"World"
},
{
"Key":"Key2",
"Value":"Hello"
}]
以下是设置为 WebMessageFormat.Xml 时示例请求的 Xml 内容
<ArrayOfKeyValueOfstringstring xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<KeyValueOfstringstring>
<Key>Key1</Key>
<Value>Hello</Value>
</KeyValueOfstringstring>
<KeyValueOfstringstring>
<Key>Key2</Key>
<Value>World</Value>
</KeyValueOfstringstring>
</ArrayOfKeyValueOfstringstring>
如果问题仍然存在,请随时告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.