代码之家  ›  专栏  ›  技术社区  ›  developer

WCF REST格式输出

  •  0
  • developer  · 技术社区  · 5 年前

    我有一个使用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"
        }
      ]
    }
    

    我怎样才能改变现状 Id 在内部 DataList DataID ? 我试过了 [DataMember(Name = "DataID")] 但它不起作用。我不想将c#属性更改为 数据标识 让它发挥作用。

    1 回复  |  直到 5 年前
        1
  •  0
  •   developer    5 年前

    找到原因,我必须申报 Data 类为 [DataContract] .

    推荐文章