简体   繁体   中英

Is there a way to map a virtual directory for *.asmx methods in ASP.Net 2.0?

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/MethodName and ~/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?

I was surprised to find out that you're correct about this limitation, however it applies only to .asmx Script Services. You can still return any kind (and combination) of content-types you want with ASP.NET 2.0+ in general.

I found this article that explains these limitations and clarifies misconceptions about ASMX/JSON: http://encosia.com/asmx-and-json-common-mistakes-and-misconceptions/ . The relevant excerpt says:

"As I alluded to earlier, the one stipulation is that these ScriptServices only return JSON serialized results if they are requested properly. Otherwise, even a service marked with the attribute will return XML instead of JSON. I can only assume that's part of the reason for the misconception that ASMX services cannot respond with JSON."

+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 a HashTable (JSON), I instead wrote out a custom return object 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

    <WebMethod()> _
    <ScriptMethod()> _
    Public Function MethodName As CustomReturnObject
        ...
    End Function

End Class

I made sure that the custom return object implemented IXmlSerializable and now the Framework returns XML or JSON depending on the caller.

If the caller specifies a contentType of application/xml , XML is returned.

If the caller specifies a contentType of application/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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM