0

I have an ASP.NET web application.

It has a web service, with several web methods.

All of these web methods are based on the default settings. For instance:

using System.Web.Services;

namespace WebApplication2
{
    [WebService(Namespace = "http://mydomain.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class WebService1 : WebService
    {
        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod]
        public Person GetPersonById(int id)
        {
            Person result = new Person();
            // code...
            return person;
        }
    }
}

The response is in SOAP (XML) format.

My question: Can I change the response's format to JSON, based on an input parameter or on a header?

Ron Klein
  • 9,178
  • 9
  • 55
  • 88
  • Are you asking _how_ to do this, or if you _should_ do this? – Oded Nov 11 '10 at 20:15
  • @Oded - I'm asking if it's technically possible, and if so - how can it be done. The "should" part is not in the scope of this question, since there are a few limitations about this service at production environment. – Ron Klein Nov 11 '10 at 21:07
  • Ron if any of the responses is the answer please mark it as the answer – Shiv Kumar Nov 14 '10 at 17:08

3 Answers3

0

You should check out Windows Communication Foundation (WCF). Using that you can define several endpoints for a service. Each one of these endpoints can, for example, return the data in different format.

REST / SOAP endpoints for a WCF service

Community
  • 1
  • 1
Ozzy
  • 1,712
  • 11
  • 14
0

Technically yes, you simply send back the Json and set the Content-Type of the Response to "application/json".

Shiv Kumar
  • 9,599
  • 2
  • 36
  • 38
  • Does this technique apply also to non-string return type? (see the second method) – Ron Klein Nov 12 '10 at 04:44
  • When you return JSON from a server to a client it is always in the forms of a string. I mean JSON is a string representation of Javascript objects, that is it is not binary. Sothe short answer is no, however, since for the most part SOAP is "string" based and in your case your Person objects gets serialized to a string it's a matter of havint it serialized to a JSON string. It's been a while since I used WebServices, but in a HttpHandler for example, if I wanted to send back a JSON of a Person object you can use the DataContractJsonSerializer class to do the job. – Shiv Kumar Nov 12 '10 at 05:29
  • Note that SOAP/XML and JSON are competing concepts in many ways. So in your base because you're using WebServices you don't have a choice to send back JSON. – Shiv Kumar Nov 12 '10 at 05:30
  • So if I were doing this in an IHttpHandler for example I could easily switch between and XML response or a JSON response or any other kind of response. The same with WCF, but there is quite a bit of a learning curve with WCF – Shiv Kumar Nov 12 '10 at 05:33
0

The response type of a ASMX Web Service is specified by the ResponseFormat attribute on the individual web method.

E.g:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
public Person GetPersonById(int id)

or

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public Person GetPersonById(int id)

So AFAIK the answer is no - you cannot return both (from one method that is).

I'm sure there are some hacks you can do, but this is the recommended way.

If you want to start returning both types, you should move to a more RESTful approach, either with WCF REST, OData or ASP.NET MVC.

In those technologies, the callee can specify the response type they wish:

GET: http://api.yourdomain.com/person/1?format=json

GET: http://api.yourdomain.com/person/1?format=xml

Note how both calls are one physical resource.

On a side note, your JSON web service calls should be HTTP POST, for security reasons mentions by "The Goo" here.

Community
  • 1
  • 1
RPM1984
  • 72,246
  • 58
  • 225
  • 350