【发布时间】:2018-04-19 06:59:43
【问题描述】:
我正在尝试将一个对象发布到我的 API,但它无法在那里使用。
客户代码
const url = '/odata/StammVersicherter';
const headers = new Headers();
headers.append('Content-Type', 'application/json');
const requestBody = JSON.stringify(data);
return this._http.post(url, requestBody , { headers: headers }).map(
res => {
// do something with the response
}
);
API 代码
public async Task<IHttpActionResult> Post([FromBody]Data data)
{
string content = await this.Request.Content.ReadAsStringAsync(); // empty!
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
_dataContext.Data.Add(data);
await _dataContext.SaveChangesAsync();
return Created(versicherter);
}
当此调用到达我的 api 方法时,Data 对象为空。有什么快速的想法吗?
【问题讨论】:
-
我显然没有给予应有的关注。反序列化已开始,但完成时出现错误。 (无法转换为数字的字符串)。
标签: post asp.net-web-api http-post