Haddar Macdasi
Haddar Macdasi

Reputation: 3555

asp.net webservice return json type

useing asp.net webservice with method :

[WebMethod(EnableSession = true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public List<MyObject> GetList()
{
    ....return new List of MyObject{ x =  .., y = .. , z = ..};
}

the client using that service is with JQuery Ajax call is working good

$.ajax({
        type: "POST",
        url: url,
        data: data == null ? "{}" : data,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
 ..... and so on ... 

but with firebug I've notice that the response is :

{"d":[{"__type":"Common.MyObject","z":"2000","x":1500,"y":1000,"a":"0"},{"__type":"Common.MyObject","z":"2000","x":1455,"y":1199.57,"a":"1"} ...... and so on ]}

1) question is why do I need this ""__type":"Common.MyObject" ?
2) I want to remove it so the response will be smaller , how can I do it ?

Upvotes: 0

Views: 1464

Answers (3)

Dom
Dom

Reputation: 40501

Hey sorry for the late reply! I recently was experiencing a similar problem. I was able to fix it without rewriting my service by doing the following:

  • go to your web.config file

  • locate the behavior.

    In my case, behavior was called "Project1.Services.DataTableAspNetAjaxBehavior" and this was found under <system.serviceModel>
    <behaviors>
    <endpointBehaviors>

PICTURE: enter image description here

  • Finally, add <webHttp defaultOutgoingResponseFormat="Json" /> to your behavior.

(NOTE: if you want to keep a wrapper, use add:
<webHttp defaultBodyStyle="Wrapped" defaultOutgoingResponseFormat="Json" />
OR
<webHttp defaultBodyStyle="WrappedResponse" defaultOutgoingResponseFormat="Json" />

I hope this helps! Happy coding!

Upvotes: 0

Haddar Macdasi
Haddar Macdasi

Reputation: 3555

ok ,I used httpmodule and regex to change the response

httpmodule by: http://bloggingabout.net/blogs/adelkhalil/archive/2009/08/14/cross-domain-jsonp-with-jquery-call-step-by-step-guide.aspx#525423

regex by : https://stackoverflow.com/a/6349813/1218546

and it works for all services methods

Upvotes: 1

Kevin Junghans
Kevin Junghans

Reputation: 17540

It works fine for me when I have the web service configured like this:

[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
class MyObjectService
{
    [OperationContract]
    [WebGet(UriTemplate = "MyObjects", ResponseFormat = WebMessageFormat.Json)]
    public IEnumerable<MyObect> GetAlMylObjects()
    {
        MyObjectMgr objectMgr = new MyObjectMgr();
        return objectMgr.GetAll();
    }

}

Here is the code for MyObjectMgr:

public class MyObjectMgr
{
    public List<MyObect> GetAll()
    {
        List<MyObect> objList = new List<MyObect>();
        objList.Add(new MyObect { x = 1, y = 21, z = 33 });
        objList.Add(new MyObect { x = 4, y = 51, z = 66 });
        return objList;

    }
}

And here is what the response looks like:

[{"x":1,"y":21,"z":33},{"x":4,"y":51,"z":66}]

I am using a GET instead of POST, but I would not think that would make any difference. Usually for a REST API if you are just retrieving information you want to use a GET.

I have stopped using WCF for RESTful web service because it is to hard to configure and temperamental. I have started to use the ASP.NET Web API which is part of the upcoming MVC 4.0 release. It is much easier to setup RESTful API's with. You do not have to specify whether you want JSON or XML in the service. The client can specify it in the HTTP header, which is how it should work.

Upvotes: 2

Related Questions