0

I have a requirement where most of WCF REST apis are of type GET and would receive Dictionary as a param. But when it is put as below, it throws error.

[WebGet(UriTemplate = "/GetVersion?Param1={serverDetails}")]
public Version GetVersion(Dictionary<string, string> keyValue)
{

}

It gives below error:

Operation 'GetVersion' in contract 'IDeploy' has a query variable named 'keyValue' of type 'System.Collections.Generic.Dictionary'2[System.String,System.String]', but type 'System.Collections.Generic.Dictionary'2[System.String,System.String]' is not convertible by 'QueryStringConverter'. Variables for UriTemplate query values must have types that can be converted by 'QueryStringConverter'.

Any idea how to resolve this? it would be hard to replace Dictionary param type as there are lots of such methods in service.

Rufus L
  • 32,853
  • 5
  • 25
  • 38
Sachin
  • 193
  • 1
  • 9
  • [Take a look at these answers](https://stackoverflow.com/questions/11950351/method-with-dictionary-parameter-in-asp-net-web-api) – Chronicle Apr 17 '20 at 22:56

1 Answers1

0

For supporting such complex parameters, The Get request cannot accomplish it. We need Post/Put/Delete request to complete this task since we can set up these complex parameters in the request body.

[OperationContract]
        [WebInvoke(RequestFormat =WebMessageFormat.Json,BodyStyle =WebMessageBodyStyle.Bare)]
        string GetData(Dictionary<string,string> value);

Below is the JSON content of the sample request. 

[{
    "Key":"Key1",
    "Value":"World"
},
{
    "Key":"Key2",
    "Value":"Hello"
}]

Below is the Xml content of the sample request when set to WebMessageFormat.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>

Feel free to let me know if the problem still exists.

Abraham Qian
  • 6,097
  • 1
  • 3
  • 20
  • Thanks for your inputs, but cannot change the type of method. Did some changes with QueryStringConverter. – Sachin Apr 20 '20 at 16:24
  • The HttpGet request retrieves the parameter by using the query string instead of the request body, therefore, most of the RestAPI don’t support the composite type parameter in HttpGet request, including the Asp.net WebAPI and so on. https://stackoverflow.com/questions/978061/http-get-with-request-body – Abraham Qian Apr 21 '20 at 10:02