WCF REST格式输出



我有一个使用REST协议的WCF服务。

代码:

[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; }
}

目前,它返回以下输出:

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

如何将DataList中的Id更改为DataID?我试过[DataMember(Name = "DataID")],但不起作用。我不想将c#属性更改为DataID以使其工作。

找到原因,我不得不将Data类声明为[DataContract]

最新更新