views:

2208

answers:

2

I recently saw a jQuery example where a POST was made to "Default.aspx/Test", where Test was a WebMethod in Default.aspx, and the content-type for the request was "application/json".

The reply from the WebMethod was in JSON. I always thought WebMethods returned SOAP responses, but if I'm interpreting this code correctly, like I said, the WebMethod returns JSON.

Is this correct? Do WebMethods return a response in the format of the request content-type? Since when has this been possible? Always? Or is this because I have ASP.NET AJAX installed? If so, what namespaces can I disassemble to see how this works?

I've only used WebMethods for .NET to .NET communication, which has been in SOAP, and always written custom code to respond in the correct format (SOAP, JSON, or XML), based on a request query string parameter specifying the desired format. I always thought WCF was required for this kind of metaformat functionality.

+1  A: 

It is a part of ASP.NET AJAX. See for example Using Web Services in ASP.NET AJAX. Part of the AJAX client architecture goes like this:

Call Web services by using the HTTP POST verb. A POST request has a body that contains the data that the browser sends to the server. It does not have a size limitation. Therefore, you can use a POST request when the size of the data exceeds the intrinsic size limitation for a GET request. The client serializes the request into JSON format and sends it as POST data to the server. The server deserializes the JSON data into .NET Framework types and makes the actual Web service call. During the response, the server serializes the return values and passes them back to the client, which deserializes them into JavaScript objects for processing.

gimel