WCF REST Format Output

23 Views Asked by At

I have a WCF service using REST protocol.

Code:

[ServiceContract]
public interface IHybridService
{

    [OperationContract]
    [WebInvoke(Method = "GET", UriTemplate = "/hybridservice/compositedata/{value}", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Bare)]
    CompositeType GetDataUsingDataContract(string value);
}

[DataContract]
public class CompositeType
{
    List<Data> data = new List<Data>();

    public CompositeType()
    {
        data.Add(new Data() { Id= 1, Value = "test1" });
    }

    [DataMember]
    public List<Data> DataList
    {
        get { return data; }
        set { data = value; }
    }
}
public class Data
{
    [DataMember(Name = "DataID")]
    public int Id { get; set; }

    public string Value { get; set; }
}

Currently it returns the following output:

{
  "DataList": [
    {
      "Id": 1,
      "Value": "test1"
    }
  ]
}

How can I change the Id within DataList to DataID? I tried [DataMember(Name = "DataID")] but it doesn't work. I do not want to change the c# property to DataID to make it work.

1

There are 1 best solutions below

0
developer On

Found the reason, I had to declare Data class as [DataContract].