0

I have the following web method:

    <WebMethod()> _
    <ScriptMethod(ResponseFormat:=ResponseFormat.Json, UseHttpGet:=True, XmlSerializeString:=True)> _
    Public Function GetDictionary() As Dictionary(Of String, String)

        Dim d As New Dictionary(Of String, String)
        d.Add("key1", "value1")
        d.Add("key2", "value2")
        Return d

    End Function

I can retrieve the results fine (JSON) if I use HttpPost from my ajax call, but as soon as I use HttpGet I get the following exception:

System.NotSupportedException: The type System.Collections.Generic.Dictionary`2[[System.String, mscorlib , Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version =2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] is not supported because it implements IDictionary

I wanted to use HttpGet here so that the result can be cached.

I tried every variation of calling this, but no luck. Any ideas? Is this possible with GET?

1
  • For anyone coming here recently I believe you need to set the Content-Type: request header to application/json. Commented May 31, 2023 at 22:16

2 Answers 2

1

I'm a little confused - if the ResponseFormat is JSON (as in your example above) then IDictionary derivatives should be supported, however if it is XML then I could understand seeing this error as the XmlSerializer does not support this.

One option to send a dictionary type using the XmlSerializer is to implement logic to convert it to an array or List or ArrayList. Alternatively you could implement a custom serializer for the data and write your own XML, returning an XmlDocument from your method instead. This would allow you to format the data in any way you choose.

Maybe you could clarify if you are using JSON or XML?

Sign up to request clarification or add additional context in comments.

1 Comment

I'm using JSON, but XML didn't work either (unless I was missing something)
1

Another alternative is to change the Return type to a String and then convert the Dictionary to JSON via JavaScriptSerializer.Serialize. This may not be exactly what you were intending with returning a Dictionary but it will be key=value pairs in the JSON Response.

<WebMethod()> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json, UseHttpGet:=True, XmlSerializeString:=True)> _
Public Function GetDictionary() As String

    Dim d As New Dictionary(Of String, String)
    d.Add("key1", "value1")
    d.Add("key2", "value2")
    Return New JavaScriptSerializer().Serialize(d)

End Function

And the resulting JSON:

{"key1":"value1","key2":"value2"}

1 Comment

This is also a good option. It doesn't explain why POST works and GET doesn't, however.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.