【发布时间】:2017-05-31 21:44:07
【问题描述】:
我正在尝试进行 PUT Api 调用并通过一个对象。 在前端,当我在执行 API 调用之前 console.log 对象时,它读取的值就好了......
但是,当它到达 .Net Core 控制器中的断点时,无论我尝试什么,该值始终为 null。
Angular/客户端
public edit(message) {
console.log(message);
this.$http.put(`api/causemessage`, message).then((res) => {
this.$state.reload();
});
}
.Net 核心控制器
[HttpPut]
public void Put([FromBody] CauseMessage msg)
{
_service.EditCauseMessage(msg);
//TODO : Get the PUT to get the correct data from the client side. Client side "should" be sending the
//TODO : correct data already.
}
^ msg 似乎始终为空,即使在客户端中的 api 调用之前,日志显示它已填充数据。
开发者工具 - 控制台日志
Object {id: 1004, message: "Testing123", messageDate: "Today", causeId: "d5a93ae4-f248-439a-9425-8be7746591fc", $$hashKey: "object:15"}
$$hashKey
:
"object:15"
causeId
:
"d5a93ae4-f248-439a-9425-8be7746591fc"
id
:
1004
message
:
"Testing123"
messageDate
:
"Today"
如果有人能帮我找出为什么我总是收到空值,那就太好了。
【问题讨论】:
-
将内容类型设置为 json 并确保在发送时 JSON.stringyfy 负载
-
我最终弄明白了。我需要使用正确的参数创建一个 DTO,以便数据绑定工作。