【问题标题】:ASP.NET Core Controller PUT parameter always nullASP.NET Core Controller PUT 参数始终为空
【发布时间】: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,以便数据绑定工作。

标签: angular asp.net-core-mvc


【解决方案1】:

需要确保我有一个正确的 DTO 来进行数据绑定。 我有 5 个属性,而不是从客户端发送的 4 个。

对不起!但感谢您的回答!

【讨论】:

    猜你喜欢
    • 2016-11-22
    • 2017-06-07
    • 2019-04-15
    • 2017-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多