2

I'm developing a WCF web service that returns this:

{
    "allFormsResult": [
        {
            "FormId": 1,
            "FormName": "Formulario 1"
        },
        {
            "FormId": 2,
            "FormName": "Formulario 2"
        },
        {
            "FormId": 3,
            "FormName": "Formulario 3"
        }
    ]
}

This is the code:

public class RestServiceImpl : IRestServiceImpl
    {
        public List<FormContract> allForms()
        {
            List<FormContract> list = null;
            using (var vAdmEntities = new ADMDatabase.ADMEntities())
            {
                list = new List<FormContract>();
                foreach (var form in vAdmEntities.Form)
                {
                    FormContract formC = new FormContract
                    {
                        FormName = form.name.Trim(),
                        FormId = form.formId
                    };
                    list.Add(formC);
                }
            }

            return list;
        }
    }

How can I do to generate it in this way?

[
    {
        "FormId": 1,
        "FormName": "Formulario 1"
    },
    {
        "FormId": 2,
        "FormName": "Formulario 2"
    },
    {
        "FormId": 3,
        "FormName": "Formulario 3"
    }
]
1
  • Not sure but try returning FormContract[] instead of List<FormContract>. Commented Apr 9, 2012 at 13:30

2 Answers 2

4

The problem is here:

namespace ADM
{
    [ServiceContract]
    public interface IRestServiceImpl
    {
        [OperationContract]
        [WebInvoke(Method = "GET",
            ResponseFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Wrapped,
            UriTemplate = "forms/")]
        List<FormContract> allForms();
    }
}

I have to use it this way:

namespace ADM
{
    [ServiceContract]
    public interface IRestServiceImpl
    {
        [OperationContract]
        [WebInvoke(Method = "GET",
            ResponseFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Bare,
            UriTemplate = "forms/")]
        List<FormContract> allForms();
    }
}

Changing BodyStyle:

BodyStyle = WebMessageBodyStyle.Bare
Sign up to request clarification or add additional context in comments.

Comments

0

This behavior can also be set as default through the Web.Config, without the need to add the attributes directly to the contract.

<services>
  <service name="MyServiceNameSpace.MyServiceClass">
    <endpoint
        address="http://yourservicedomain.ext/MyServiceClass.svc/"
        binding="webHttpBinding"
        contract="MyServiceNameSpace.MyServiceContract"
        behaviorConfiguration="MyEndpointBehavoir"
        listenUri="/" />        
  </service>      
</services>

<behaviors>
  <endpointBehaviors>
    <behavior name="MyEndpointBehavoir">
      <webHttp defaultOutgoingResponseFormat="Json" defaultBodyStyle="Bare"/>
    </behavior>        
  </endpointBehaviors>
</behaviors>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.