调用Web API进行后期操作时未解析数据协定

时间:2018-06-27 11:12:06

标签: c# asp.net-web-api datacontract datamember

我有两个API,WebAPIB调用WebAPIA并返回结果。 由于某些限制,我无法在WebAPIA中更改Employee类,并且其Id属性的名称为“ a”,因此我创建了新的Employee类以在Web API B中接收对象。获取操作Json属性有效,但是调用post时Web API A中未为ID属性解析的值。

--- Web API A -----

发布方法->

// POST api/values
 public Employee Post([FromBody]Employee value)
 {
    var val = value;          
    return val;
  }

Web API A中的员工类->

[DataContract]
public class Employee
{
        [DataMember(Name ="a")]
        public int Id { get; set; }

        [DataMember]
        public string Name { get; set; }
}

--- Web API A -----

--- Web API B -----

Post方法以调用Web API A->

// POST api/values
public async Task<Employee> Post([FromBody]Employee value)
{
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("http://localhost/WebAPIA/api/values");
                HttpResponseMessage response = await client.PostAsJsonAsync<Employee>(commonWebApiUrl, value).ConfigureAwait(false);

                if (response.IsSuccessStatusCode)
                {
                    var mailingList = await response.Content.ReadAsAsync<Employee>();
                    if (mailingList == null)
                        throw new ArgumentNullException(nameof(mailingList));
                    return mailingList;
                }
                else
                {
                    throw new HttpException(response.ReasonPhrase);
                }
            }
}

Web API B中的雇员类->

[DataContract]
 public class Employee
 {
        [DataMember]
        [JsonProperty(PropertyName ="a",ItemTypeNameHandling =TypeNameHandling.None)]
        public int Id { get; set; }

        [DataMember]
        public string Name { get; set; }
    }

--- Web API B -----

0 个答案:

没有答案