A little while ago, I managed to get WebServices to return JSON as well as XML from the code-behind of an ASPX.
Today, I needed to migrate an existing ASMX WebService to return JSON instead of XML (which was accomplished with little fuss). My problem became evident in testing the new method.
In the code-behind for the ASMX, I created a new method to return JSON instead of XML.
The method works, in that it returns the expected JSON however the new routine breaks every XML-returning method in the code-behind (they all throw “System.NotSupportedException: The type System.Collections.Hashtable is not supported because it implements IDictionary.” which is unexpected because the return types are all defined as XmlDocument).
If I simply comment out the JSON-returning method, the XML-returning methods function normally. Uncommenting out the JSON-returning method breaks the XML-returning methods again.
While I cannot find any supporting documentation anywhere, this behavior leads me to believe that in ASP.Net 2.0, JSON-returning methods cannot co-exist in the same module as XML-returning methods.
This led me to try separating the methods in sub-classes like so:
<WebService(Namespace:="http://tempura.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ScriptService()> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class MyWebServices
Inherits System.Web.Services.WebService
<ScriptService()> _
Public Class xml
Inherits System.Web.Services.WebService
<WebMethod()> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Xml)> _
Public Function MethodName As XmlDocument
...
End Function
End Class
<ScriptService()> _
Public Class json
Inherits System.Web.Services.WebService
<WebMethod()> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
Public Function MethodName As Hashtable
...
End Function
End Class
End Class
The idea being that I could then call the services in one of the following manners:
~/MyWebServices.asmx/xml/MethodName~/MyWebServices.asmx/xml.MethodName~/MyWebServices.asmx/json/MethodName~/MyWebServices.asmx/json.MethodName
This unfortunately didn’t work, giving a 404-Not Found error.
I could simply throw all the JSON-returning methods into a different ASMX, but I am hoping for a better way.
My questions are thus:
- Is there a way to accomplish
~/MyWebServices.asmx/xml/MethodNameand~/MyWebServices.asmx/json.MethodName? I like the idea of separating functions by return type in a virtual path. - Failing that, is there a way to get JSON-returning methods to co-exist in the same module as XML-returning methods?
+1 to @smartcaveman for the attempt.
The solution to this was a different (and I think better, overall) approach. Instead of explicitly returning either an
XMLDocument(XML) or aHashTable(JSON), I instead wrote out a custom return object like so:I made sure that the custom return object implemented
IXmlSerializableand now the Framework returns XML or JSON depending on the caller.If the caller specifies a
contentTypeofapplication/xml, XML is returned.If the caller specifies a
contentTypeofapplication/json, JSON is returned.I think this approach is better in that I don’t have to organize WebServices by return type, nor do I have to maintain two separate methods (one XML, one JSON) for each WebService I write.
Thanks for at least looking.